react-native-branch-deep-linking-attribution icon indicating copy to clipboard operation
react-native-branch-deep-linking-attribution copied to clipboard

RCTLinkingManager vs RNBranch in AppDelegate.m

Open sergiulucaci opened this issue 4 years ago • 3 comments

Hei BranchIO team.

This is more like a doc-confusion which might be useful for others, that's why I am opening an issue.

I have RCTLinkingManager set up in AppDelegate.m for both deep and universal links.

Question 1 If I want to use only RNBranch manager I assume I would remove RCTLinkingManager at all from this file, right?

Question 2 But what if I were to use both RCTLinkingManager and RNBranch instead?

This is how it looks before BranchIO:

// Respond to URI scheme links
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

// Respond to Universal Links
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
 restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
  return [RCTLinkingManager application:application
                   continueUserActivity:userActivity
                     restorationHandler:restorationHandler];
}

Should it look like this?

// Respond to URI scheme links
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RNBranch application:app openURL:url options:options] ||
    [RCTLinkingManager application:application openURL:url options:options];
}

// Respond to Universal Links
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
 restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
  return [RNBranch continueUserActivity:userActivity] ||
    [RCTLinkingManager application:application
                   continueUserActivity:userActivity
                     restorationHandler:restorationHandler];
}

Thank you!

sergiulucaci avatar Jul 10 '20 12:07 sergiulucaci

Would also like to know this as ios won't build for me while both exist together and I don't see mention of this anywhere in the docs. In fact it was this issue that clued me in to what the problem is. I'm using react-native-branch 5.0.0 and react native 0.63.

Before in my AppDelegate.m file I had this:

- (BOOL)application:(UIApplication *)application
   openURL:(NSURL *)url
   options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:application openURL:url options:options];
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
 restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
 return [RCTLinkingManager application:application
                  continueUserActivity:userActivity
                    restorationHandler:restorationHandler];
}

I added the following before @end as suggested in both the docs and the webview_tutorial github:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    if ([RNBranch application:app openURL:url options:options])  {
        // do other deep link routing for the Facebook SDK, Pinterest SDK, etc
    }
    return YES;
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
    return [RNBranch continueUserActivity:userActivity];
}

but then I get when I try to build:

/Users/ryan/code/babblymobile/ios/Babbly/AppDelegate.m:116:1: error: duplicate declaration of method 'application:openURL:options:'
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
^
/Users/ryan/code/babblymobile/ios/Babbly/AppDelegate.m:101:1: note: previous declaration is here
- (BOOL)application:(UIApplication *)application
^
/Users/ryan/code/babblymobile/ios/Babbly/AppDelegate.m:123:1: error: duplicate declaration of method 'application:continueUserActivity:restorationHandler:'
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
^
/Users/ryan/code/babblymobile/ios/Babbly/AppDelegate.m:108:1: note: previous declaration is here
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity
^

Should I just remove RCTLinkingManager altogether? Does that have other implications for React Native Linking? Should I use || like in the OP?

ryanbabbly avatar Sep 10 '20 21:09 ryanbabbly

This solves the issue. https://stackoverflow.com/questions/38945790/appdelegate-m-for-both-fbsdk-and-linkingmanager/38945881

dibakar95 avatar Sep 24 '20 04:09 dibakar95

Pasting this here so people don't have to click through to StackOverflow.

[RNBranch application:application openURL:url options:options]; returns a BOOL, if that is true then Branch handled the link and you should exit out of the function returning True/Yes if not, pass the app/options to your other link handlers.

Should look something like this:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  BOOL branchOpened = [RNBranch application:application openURL:url options:options];
  if (branchOpened){
    return YES;
  }
  // Required for opening links from JS
  return [RCTLinkingManager application:application openURL:url options:options];
}

SeanDunford avatar Jul 19 '22 20:07 SeanDunford