Skip to content

Instantly share code, notes, and snippets.

@0xpatrickdev
Created December 6, 2018 01:55
Show Gist options
  • Save 0xpatrickdev/a03255c4d45c771075367ab6b6f9b828 to your computer and use it in GitHub Desktop.
Save 0xpatrickdev/a03255c4d45c771075367ab6b6f9b828 to your computer and use it in GitHub Desktop.
Handle deep liking from multiple sources in a React Native app for iOS
// 'The AppDelegate.m file can only have one method for openUrl.'
// 'If you're also using RCTLinkingManager to handle deep links, you should handle both results in your openUrl method.'
// https://github.com/facebook/react-native-fbsdk#32-ios-project
#import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <CodePush/CodePush.h>
#import <react-native-branch/RNBranch.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import <RNGoogleSignin/RNGoogleSignin.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Initialize Branch SDK
[RNBranch initSessionWithLaunchOptions:launchOptions isReferrable:YES];
// Initialize FBSDK
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
// Declare the location of the js bundle for react-native (CodePush)
NSURL *jsCodeLocation;
#ifdef DEBUG
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
jsCodeLocation = [CodePush bundleURL];
#endif
// set up the root view for react-native
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"myapp" initialProperties:nil launchOptions:launchOptions];
// Set the background color of the root view
rootView.backgroundColor = [[UIColor alloc] initWithRed:0.09 green:0.09 blue:0.11 alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// handle incoming links from Branch (deep-linking content)
BOOL handledBranch = [RNBranch.branch
application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
// handle incoming links from Facebook (sign-in)
BOOL handledFB = [[FBSDKApplicationDelegate sharedInstance]
application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
// handle incoming links from Google (sign-in)
BOOL handledGoog = [RNGoogleSignin
application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation
];
return handledBranch || handledFB || handledGoog;
}
// set up fb app events
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
// Add the continueUserActivity function for hte Branch SDK
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
return [RNBranch continueUserActivity:userActivity];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment