thanhpcc96 / react-native-facebook-account-kit

A Facebook Account Kit SDK wrapper for React Native

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-native-facebook-account-kit

A Facebook Account Kit SDK wrapper for react-native.

We will support the latest version of React Native on each release. For older RN versions you may use an older version of this module compatible with that RN version.

Cài đặt

Thực hiện command để add vào dependencies

npm install --save react-native-facebook-account-kit
react-native link react-native-facebook-account-kit

Nếu bạn thích bản fork của tôi( thanhpcc96) có tùy chỉnh dành riêng cho VN

    yarn add https://github.com/thanhpcc96/react-native-facebook-account-kit.git

Cấu hình

Cho iOS

  1. Đầu tiên, link the AccountKit SDK vào project vào project của bạn theo các bước như sau:
  • Mở project với Xcode ( trong thư mục ios)
  • Mở thư mục node_modules/react-native-facebook-account-kit/ios với Finder( MAC) hoặc Folder(Win)
  • Kéo hoặc thả AccountKit.frameworkAccountKitStrings.bundle từ Finder vào project trong Xcode (Chú Ý Quan Trọng: lựa chọn "Copy items if needed")
  1. Thêm Facebook credentials vào trong file Info.plist
    <plist version="1.0">
      <dict>
        ...
        <key>FacebookAppID</key>
        <string>{Id-của-app}</string>
        <key>AccountKitClientToken</key>
        <string>{account-kit-client-token-của-bạn}</string>
        <key>CFBundleURLTypes</key>
        <array>
          <dict>
            <key>CFBundleURLSchemes</key>
            <array>
              <string>ak{Id-của-app}</string>
            </array>
          </dict>
        </array>
        ...
      </dict>
    </plist>
  1. Xong! Bây giờ bạn có thể chạy project trong Xcode hoặc react-native run-ios từ terminal

This is the minimal required configuration. Take a look to the Account Kit documentation for iOS for a more detailed guide.

For Android

  1. In android/app/src/main/res/values/strings.xml

    ...
    <string name="fb_app_id">YOUR_FACEBOOK_APP_ID</string>
    <string name="ak_client_token">YOUR_CLIENT_TOKEN</string>
  2. In android/app/src/main/AndroidManifest.xml

...
<application>

    ...
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/fb_app_id" />
    <meta-data
        android:name="com.facebook.accountkit.ApplicationName"
        android:value="@string/app_name" />
    <meta-data
        android:name="com.facebook.accountkit.ClientToken"
        android:value="@string/ak_client_token" />
 </application>
 ...
  1. Check the RNAccountKitPackage is registered in your MainApplication.java file. This should be done by react-native link but sometimes it doesn't. You should see something like the following
@Override
protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        new RNAccountKitPackage() // <-- the package is registered
    );
}

This is the minimal required configuration. Take a look to the Account Kit documentation for Android for a more detailed guide.

Themes

Regarding the theming, Account Kit way to customize styles differs between platforms.

iOS

For iOS you must use the field theme on the configure payload and set the properties you want to overwrite.

import RNAccountKit, {
  Color,
  StatusBarStyle,
} from 'react-native-facebook-account-kit'

RNAccountKit.configure({
  ...,
  theme: {
    // Background
    backgroundColor: Color.rgba(0, 120, 0, 0.1),
    backgroundImage: 'background.png',
    // Button
    buttonBackgroundColor: Color.rgba(0, 153, 0, 1.0),
    buttonBorderColor: Color.rgba(0, 255, 0, 1),
    buttonTextColor: Color.rgba(0, 255, 0, 1),
    // Button disabled
    buttonDisabledBackgroundColor: Color.rgba(100, 153, 0, 0.5),
    buttonDisabledBorderColor: Color.rgba(100, 153, 0, 0.5),
    buttonDisabledTextColor: Color.rgba(100, 153, 0, 0.5),
    // Header
    headerBackgroundColor: Color.rgba(0, 153, 0, 1.0),
    headerButtonTextColor: Color.rgba(0, 153, 0, 0.5),
    headerTextColor: Color.rgba(0, 255, 0, 1),
    // Input
    inputBackgroundColor: Color.rgba(0, 255, 0, 1),
    inputBorderColor: Color.hex('#ccc'),
    inputTextColor: Color.hex('#0f0'),
    // Others
    iconColor: Color.rgba(0, 255, 0, 1),
    textColor: Color.hex('#0f0'),
    titleColor: Color.hex('#0f0'),
    // Header
    statusBarStyle: StatusBarStyle.LightContent, // or StatusBarStyle.Default
  }
})

To see the statusBarStyle reflected you must set the UIViewControllerBasedStatusBarAppearance property to true on your app's Info.plist file. You can do it from XCode screen shot 2016-08-02 at 11 44 07 am

Android

For Android you need to follow this steps:

Check this commit to see how it's done in our sample app

  1. In your application styles.xml file (usually located in android/app/src/main/res/values folder) create a Theme with the following schema
<style name="LoginThemeYellow" parent="Theme.AccountKit">
    <item name="com_accountkit_primary_color">#f4bf56</item>
    <item name="com_accountkit_primary_text_color">@android:color/white</item>
    <item name="com_accountkit_secondary_text_color">#44566b</item>
    <item name="com_accountkit_status_bar_color">#ed9d00</item>

    <item name="com_accountkit_input_accent_color">?attr/com_accountkit_primary_color</item>
    <item name="com_accountkit_input_border_color">?attr/com_accountkit_primary_color</item>
</style>

See the full set of customizable fields here

  1. In your app AndroidManifest.xml file (usually under android/app/src/main folder) set that Theme to the AccountKitActivity
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" <-- Add this line
    ...>

    <!-- Set the AccountKitActivity theme -->
    <activity
      tools:replace="android:theme"
      android:name="com.facebook.accountkit.ui.AccountKitActivity"
      android:theme="@style/LoginThemeYellow" />

</manifest>

Usage

If you have issues with the method signatures you may be using an older version. Check the Releases Notes for breaking changes

import RNAccountKit from 'react-native-facebook-account-kit'

// Shows the Facebook Account Kit view for login via SMS
RNAccountKit.loginWithPhone()
  .then((token) => {
    if (!token) {
      console.log('Login cancelled')
    } else {
      console.log(`Logged with phone. Token: ${token}`)
    }
  })

// Shows the Facebook Account Kit view for login via email
RNAccountKit.loginWithEmail()
  .then((token) => {
    if (!token) {
      console.log('Login cancelled')
    } else {
      console.log(`Logged with email. Token: ${token}`)
    }
  })

// Logouts the user, so getCurrentAccessToken() will retrieve null
RNAccountKit.logout()
  .then(() => {
    console.log('Logged out')
  })

// Retrieves the logged user access token, if any user is logged
RNAccountKit.getCurrentAccessToken()
  .then((token) => {
    console.log(`Current access token: ${token}`)
  })

// Retrieves the logged user account info, if any user is logged
RNAccountKit.getCurrentAccount()
  .then((account) => {
    console.log(`Current account: ${account}`)
  })

// Configures the SDK with some options
RNAccountKit.configure({
  responseType: 'token'|'code' // 'token' by default,
  titleType: 'login',
  initialAuthState: '',
  initialEmail: 'some.initial@email.com',
  initialPhoneCountryPrefix: '+1',
  initialPhoneNumber: '123-456-7890',
  facebookNotificationsEnabled: true|false, // true by default
  readPhoneStateEnabled: true|false, // true by default,
  receiveSMS: true|false, // true by default,
  countryWhitelist: ['AR'], // [] by default
  countryBlacklist: ['US'], // [] by default
  defaultCountry: 'AR',
  theme: {...} // for iOS only, see the Theme section
})

Examples

Try the examples. They are related to different React Native versions.

git clone https://github.com/underscopeio/react-native-facebook-account-kit
cd react-native-facebook-account-kit/examples/RN033 # or /RN024
npm install
react-native run-ios # or react-native run-android

License

License is MIT

About

A Facebook Account Kit SDK wrapper for React Native


Languages

Language:Objective-C 75.3%Language:Java 15.5%Language:JavaScript 8.4%Language:Ruby 0.7%