React Native
配置 Branch
- Configure your Default Link Settings
安装 Branch
via Pure React Native App
Expo Note: If you are working with Expo, you must first eject your app from Expo, use the Expo Bare Workflow and follow the instructions below.
expo eject
注意
react-native-branch requires react-native >= 0.60
- 安装模块
npm install [email protected]
yarn add [email protected]
via Native iOS app with CocoaPods
platform :ios, '9.0'
target 'APP_NAME' do
# if swift
use_frameworks!
pod 'react-native-branch', path: '../node_modules/react-native-branch'
end
- 运行
pod install
to regenerate thePods
project with the new dependencies. Note that the location ofnode_modules
relative to yourPodfile
may vary.
(可选)将 branch.json 文件添加到您应用的根目录(package.json 旁边)。
You can configure the contents at any time, but it must be present when you run
react-native link
in order to be automatically included in your native projects. This allows you to configure certain behaviors that otherwise require native code changes. See https://rnbranch.app.link/branch-json for full details on the branch.json file.
配置应用
iOS
Android
初始化 Branch
iOS
- In your app's
AppDelegate file
:
import RNBranch
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// Initialize the Branch Session at the top of existing application:didFinishLaunchingWithOptions:
func application(_ application: UIApplication, didFinishLaunchingWithOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Uncomment this line to use the test key instead of the live one.
// RNBranch.useTestInstance()
RNBranch.initSession(launchOptions: launchOptions)
return true
}
// Add the openURL and continueUserActivity functions
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return RNBranch.application(app, open:url, options:options)
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
return RNBranch.continue(userActivity)
}
#import "AppDelegate.h"
#import <RNBranch/RNBranch.h>
@implementation AppDelegate
// Initialize the Branch Session at the top of existing application:didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Uncomment this line to use the test key instead of the live one.
// [RNBranch useTestInstance];
[RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];
NSURL *jsCodeLocation;
//...
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
[RNBranch application:app openURL:url options:options];
return YES;
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
[RNBranch continueUserActivity:userActivity];
return YES;
}
@end
Android
- 将 Branch 添加到您的
MainApplication.java
// import from RNBranch
import io.branch.rnbranch.RNBranchModule;
public class MainApplication extends Application implements ReactApplication {
// add onCreate() override
@Override
public void onCreate() {
super.onCreate();
// Branch logging for debugging
RNBranchModule.enableLogging();
RNBranchModule.getAutoInstance(this);
}
// imports from RNBranch
import io.branch.rnbranch.RNBranchModule;
import io.branch.rnbranch.RNBranchPackage;
public class MainApplication extends Application implements ReactApplication {
// add RNBranchPackage to react-native package list
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNBranchPackage(),
// add onCreate() override
@Override
public void onCreate() {
super.onCreate();
// Branch logging for debugging
RNBranchModule.enableLogging();
RNBranchModule.getAutoInstance(this);
}
- 将 Branch 添加到您的
MainActivity.java
import io.branch.rnbranch.*;
import android.content.Intent;
public class MainActivity extends ReactActivity {
@Override
protected String getMainComponentName() {
return "base";
}
// Override onStart:
@Override
protected void onStart() {
super.onStart();
RNBranchModule.initSession(getIntent().getData(), this);
}
// Override onNewIntent:
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
RNBranchModule.onNewIntent(intent);
}
}
实现功能
导入 Branch
- 在任何使用 Branch SDK 的 React Native 源文件中。
import branch from 'react-native-branch'