Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Commit

Permalink
react-native-fbsdk-0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhuowen committed May 20, 2016
1 parent 42aee08 commit 1974362
Show file tree
Hide file tree
Showing 62 changed files with 490 additions and 1,238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,6 @@ public String getName() {
return "FBAppEventsLogger";
}

/**
* Notifies the events system that the app has launched & logs an activatedApp event. Should be
* called whenever your app becomes active, typically in the onResume() method of each
* long-running Activity of your app. Use HostStateModule in JS
* to listen to host life cycle event.
* Use this method if your application ID is stored in application metadata.
*/
@ReactMethod
public void activateApp() {
AppEventsLogger.activateApp(mReactContext);
}

/**
* Notifies the events system that the app has been deactivated (put in the background) and
* tracks the application session information. Should be called whenever your app becomes
* inactive, typically in the onPause() method of each long-running Activity of your app.
* Use HostStateModule in JS to listen to host life cycle event.
* Use this method if your application ID is stored in application metadata.
* @param applicationId The specific applicationId to track session information for.
*/
@ReactMethod
public void deactivateApp(@Nullable String applicationId) {
AppEventsLogger.deactivateApp(mReactContext, applicationId);
}

/**
* Set the behavior that this AppEventsLogger uses to determine when to flush logged events to
* the server. This setting applies to all instances of AppEventsLogger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.support.annotation.Nullable;
import android.util.Log;

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.annotations.ReactProp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public String getName() {
@Override
public RCTLoginButton createViewInstance(ThemedReactContext context) {
RCTLoginButton button = new RCTLoginButton(context, mCallbackManager);
mReactApplicationContext.addActivityEventListener(button);
return button;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.facebook.login.LoginBehavior;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.NativeModule;
Expand All @@ -49,7 +48,7 @@
/**
* This is a {@link NativeModule} that allows JS to use LoginManager of Facebook Android SDK.
*/
public class FBLoginManagerModule extends ReactContextBaseJavaModule implements ActivityEventListener {
public class FBLoginManagerModule extends ReactContextBaseJavaModule {

private class LoginManagerCallback extends ReactNativeFacebookSDKCallback<LoginResult> {

Expand Down Expand Up @@ -79,7 +78,6 @@ public void onSuccess(LoginResult loginResult) {
public FBLoginManagerModule(ReactApplicationContext reactContext, CallbackManager callbackManager) {
super(reactContext);
mCallbackManager = callbackManager;
reactContext.addActivityEventListener(this);
}

@Override
Expand Down Expand Up @@ -172,11 +170,6 @@ public void logInWithPublishPermissions(ReadableArray permissions, final Promise
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}

private WritableArray setToWritableArray(Set<String> set) {
WritableArray array = Arguments.createArray();
for (String e: set) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
import android.content.Intent;

import com.facebook.CallbackManager;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;

public abstract class FBSDKDialogBaseJavaModule extends ReactContextBaseJavaModule implements ActivityEventListener {
public abstract class FBSDKDialogBaseJavaModule extends ReactContextBaseJavaModule {

private CallbackManager mCallbackManager;

Expand All @@ -38,11 +37,5 @@ protected CallbackManager getCallbackManager() {
protected FBSDKDialogBaseJavaModule(ReactApplicationContext reactContext, CallbackManager callbackManager) {
super(reactContext);
mCallbackManager = callbackManager;
reactContext.addActivityEventListener(this);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
getCallbackManager().onActivityResult(requestCode, resultCode, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class FBSDKPackage implements ReactPackage {

public static String VERSION_TO_RELEASE = "ReactNative-v0.1.0";
public static String VERSION_TO_RELEASE = "ReactNative-v0.2.0";

private CallbackManager mCallbackManager;
public FBSDKPackage(CallbackManager callbackManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
Expand All @@ -42,7 +41,7 @@
* A Log In/Log Out button that maintains login state and logs in/out for the app.
* This control requires the app ID to be specified in the AndroidManifest.xml.
*/
public class RCTLoginButton extends LoginButton implements ActivityEventListener {
public class RCTLoginButton extends LoginButton {

private CallbackManager mCallbackManager;
private AccessTokenTracker mAccessTokenTracker;
Expand Down Expand Up @@ -127,11 +126,6 @@ public void onCancel() {
});
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}

private String[] setToStringArray(Set<String> set) {
String[] array = new String[set.size()];
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import com.facebook.share.model.AppInviteContent;
import com.facebook.share.model.GameRequestContent;
import com.facebook.share.model.ShareContent;
import com.facebook.share.model.ShareHashtag;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.model.ShareHashtag;
import com.facebook.share.model.ShareOpenGraphAction;
import com.facebook.share.model.ShareOpenGraphContent;
import com.facebook.share.model.ShareOpenGraphObject;
Expand Down
109 changes: 25 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# React Native FBSDK
React Native FBSDK is a wrapper around the iOS Facebook SDK and Android Facebook SDK, allowing for Facebook integration in [React Native](https://facebook.github.io/react-native/) apps. Access to native components, from login to sharing, is provided entirely through documented JavaScript modules so you don't have to call a single native function directly.
React Native FBSDK is a wrapper around the iOS Facebook SDK and Android Facebook SDK, allowing for Facebook integration in [React Native](https://facebook.github.io/react-native/) apps. Access to native components, from login to sharing, is provided entirely through documented JavaScript modules so you don't have to call a single native function directly.

Functionality is provided through one single npm package so you can use it for both platforms without downloading any extra packages. Follow this guide to use react-native-fbsdk in your react-native app.
Functionality is provided through one single npm package so you can use it for both platforms without downloading any extra packages. Follow this guide to use react-native-fbsdk in your react-native app. You can also visit https://developers.facebook.com/docs/react-native for tutorials and reference documentation.

## Installation
You need to install the sdk with [npm](https://www.npmjs.com/) and configure native Android/iOS project in the react native project.
You need to install the sdk with [npm](https://www.npmjs.com/) and configure native Android/iOS project in the react native project.
### 1. Create React Native project

First create a React Native project:
Expand All @@ -14,36 +14,19 @@ react-native init YourApp

### 2. Install JavaScript packages

Install the react-native-fbsdk package:
Install [rnpm](https://github.com/rnpm/rnpm):
```ruby
npm install --save react-native-fbsdk
```
### 3. Configure native projects
You can configure the iOS project and Android project both if you want to build a cross-platform app, or configure either side.

#### 3.1 Android project
Assuming you have [Android Studio](http://developer.android.com/sdk/index.html) installed, open the project with Android Studio and update the following files to build project with [Gradle](http://gradle.org/).

In `settings.gradle`, include the sdk subproject and specify the subproject path.
```ruby
//...
include ':react-native-fbsdk'
project(':react-native-fbsdk').projectDir = new File(settingsDir, '../node_modules/react-native-fbsdk/Android')
npm install rnpm -g
```

In `app/build.gradle`, enable multiDex support and add sdk to dependencies.
Use rnpm to install and link the react-native-fbsdk package:
```ruby
defaultConfig {
//...
multiDexEnabled true
}
//...
dependencies {
//...
compile project(":react-native-fbsdk")
}
rnpm install react-native-fbsdk
```
### 3. Configure native projects

#### 3.1 Android project
Assuming you have [Android Studio](http://developer.android.com/sdk/index.html) installed, open the project with Android Studio.
Go to `MainActivity.java` under `app/src/main/java/com/<project name>/` to complete setup.
Note that packages must be imported to use.

Expand Down Expand Up @@ -72,14 +55,6 @@ protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(packages);
}
```
Initialize sdk in method `onCreate()`.
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
}
```
Override `onActivityResult()`.
```java
@Override
Expand All @@ -88,61 +63,27 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
```
To use [AppEventsLogger](https://developers.facebook.com/docs/app-events), add method call to `activateApp`, `deactivateApp` and `onContextStop` in the corresponding life cycle events.
```java
import com.facebook.appevents.AppEventsLogger; // <--- import

@Override
protected void onResume() {
super.onResume();
AppEventsLogger.activateApp(getApplicationContext());
}

@Override
protected void onPause() {
super.onPause();
AppEventsLogger.deactivateApp(getApplicationContext());
}

@Override
protected void onStop() {
super.onStop();
AppEventsLogger.onContextStop();
}
```
Before you can run the project, follow the [Getting Started Guide](https://developers.facebook.com/docs/android/getting-started/) for Facebook Android SDK to set up a Facebook app. You can skip the build.gradle changes since that's taken care of by the rnpm link step above, but ***make sure*** you follows the rest of the steps such as calling `FacebookSdk.sdkInitialize` and updating `strings.xml` and `AndroidManifest.xml`. Note that react-native project doesn't have the Application class, so you'll need to create an implementation of the Application class yourself.

#### 3.2 iOS project
Assuming you have [CocoaPods](https://cocoapods.org) (it's recommended to use v.1.0+ `gem install cocoapods`) installed, complete the following steps:
The react-native-fbsdk has been linked by rnpm, the next step will be downloading and linking the native Facebook SDK for iOS.
Make sure you have the latest [Xcode](https://developer.apple.com/xcode/) installed. Open the .xcodeproj in Xcode found in the `ios` subfolder from your project's root directory. Now, follow ***all the steps*** in the [Getting Started Guide](https://developers.facebook.com/docs/ios/getting-started/) for Facebook SDK for iOS.

In `<project name>/ios` directory, create a `Podfile` by running:
```ruby
pod init
```

Open the generated `Podfile` and add the following code inside the **project target**:
```ruby
target 'YourApp' do
source 'https://github.com/CocoaPods/Specs.git'
pod 'React', :subspecs => ['Core', 'RCTImage', 'RCTNetwork', 'RCTText', 'RCTWebSocket'], :path => '../node_modules/react-native'
pod 'react-native-fbsdkcore', :path => '../node_modules/react-native-fbsdk/iOS/core'
pod 'react-native-fbsdklogin', :path => '../node_modules/react-native-fbsdk/iOS/login'
pod 'react-native-fbsdkshare', :path => '../node_modules/react-native-fbsdk/iOS/share'
end
```
#### 3.3 Troubleshooting
1. I cannot run the Android project.

Remove all the subprojects under `Libraries/` in Xcode. This is because React Native's iOS code will be pulled in via CocoaPods.
- Make sure you added the code snippet in step 3.1.
- Make sure you set up a Facebook app and updated the `AndroidManifest.xml` and `res/values/strings.xml` with Facebook app settings.

Run `pod install`. This will automatically download the Facebook SDK for iOS and create an Xcode workspace containing all native files. From now on open `YourApp.xcworkspace` instead of `YourApp.xcodeproj` in Xcode.
2. I get a build error stating that one of the Facebook SDK files was not found -- eg. `FBSDKLoginKit/FBSDKLoginKit.h file not found`.

Note: When doing 'pod install', warnings like `The 'YourApp [Debug]' target overrides the 'OTHER_LDFLAGS' build setting ...` may show up. To solve this, go to Xcode's target **Build Setting** section, find `Other linker flags` and add `$(inherited)` in it.
- Make sure that the Facebook SDK frameworks are installed in `~/Documents/FacebookSDK`.
- Make sure that `FBSDK[Core, Login, Share]Kit.framework` show up in the **Link Binary with Libraries** section of your build target's **Build Phases**.
- Make sure that `~/Documents/FacebookSDK` is in the **Framework Search Path** of your build target's **Build Settings**.

### 3.3 Configure native sdk
After you configure the native project in 3.1 and/or 3.2, follow the Getting Started guide for [Android](https://developers.facebook.com/docs/android/getting-started/) and [iOS](https://developers.facebook.com/docs/ios/getting-started/) sdk to set up a Facebook app. You can skip the steps that talk about downloading and linking the Facebook SDK frameworks, -- that's already taken care of by the steps above.
3. I get build errors like `Warning: Native component for "RCTFBLikeView" does not exist`:

### 3.4 Troubleshooting
If you get a build error stating that one of the Facebook SDK files was not found -- eg. `FBSDKCoreKit/FBSDKCoreKit.h` -- check two things\:
- Ensure that the Facebook SDK frameworks are installed and show up in your xcode workspace.
- Ensure the [schemes](https://developer.apple.com/library/ios/recipes/xcode_help-scheme_editor/Articles/SchemeDialog.html) (react-native-fbsdkcore, react-native-fbsdklogin, and react-native-fbsdkshare) can be run successfully.
- Make sure that `libRCTFBSDK.a` shows up in the **Link Binary with Libraries** section of your build target's **Build Phases**.

## Usage
### [Login](https://developers.facebook.com/docs/facebook-login)
Expand Down Expand Up @@ -314,9 +255,9 @@ const {
//Create response callback.
_responseInfoCallback(error: ?Object, result: ?Object) {
if (error) {
alert('Error posting data: ' + error.toString());
alert('Error fetching data: ' + error.toString());
} else {
alert('Success posting data: ' + result.toString());
alert('Success fetching data: ' + result.toString());
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sample/HelloFacebook/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* https://github.com/facebook/react-native
*/

import React, {
import React, { Component } from 'react';
import {
AppRegistry,
Component,
StyleSheet,
Text,
TouchableHighlight,
Expand Down Expand Up @@ -44,10 +44,10 @@ class HelloFacebook extends Component {
).then(
function(result) {
if (result.isCancelled) {
alert('Share cancelled');
alert('Share cancelled');
} else {
alert('Share success');
}
}
},
function(error) {
alert('Share fail with error: ' + error);
Expand Down
8 changes: 4 additions & 4 deletions Sample/HelloFacebook/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* https://github.com/facebook/react-native
*/

import React, {
import React, { Component } from 'react';
import {
AppRegistry,
Component,
StyleSheet,
Text,
TouchableHighlight,
Expand Down Expand Up @@ -44,11 +44,11 @@ class HelloFacebook extends Component {
).then(
function(result) {
if (result.isCancelled) {
alert('Share cancelled');
alert('Share cancelled');
} else {
alert('Share success with postId: '
+ result.postId);
}
}
},
function(error) {
alert('Share fail with error: ' + error);
Expand Down
Loading

0 comments on commit 1974362

Please sign in to comment.