React native deep linking vs Facebook SDK conflct

2020-08-22 04:26发布

问题:

I'm using the Facebook SDK in my react-native app, and now I want to add support for universal deep linking as well.

I modified my appdelegate per the instructions here: https://developers.facebook.com/docs/ios/getting-started/

For deep linking, I'm trying to follow this: https://facebook.github.io/react-native/docs/linking.html

However, they both seem to use the same method, and I'm not sure how to reconcile:

````

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  return [RCTLinkingManager application:application openURL:url
                      sourceApplication:sourceApplication annotation:annotation];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

````

Please let me know how I should modify the method to have both capabilities.

Thank you

回答1:

This is how I managed to solve the issue:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

  NSString *myUrl = url.absoluteString;
  if ([myUrl containsString:@"PLACE_YOUR_FB_APP_ID_HERE"]) {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                openURL:url
                                                      sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                             annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
                  ];
  } else {
    return [RCTLinkingManager application:application openURL:url options:options];
  }
}

Just replace the PLACE_YOUR_FB_APP_ID_HERE with the string from your info.plist. For example, in the following case its fb9999999999:

  <array>
    <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>fb9999999999</string>
      </array>
    </dict>
  </array>


回答2:

Ended up writing a simple if statement to see if the incoming URL was a Facebook one, and returning the FBSDKApplicationDelegate if it was, and the RCTLinkingManager object if it wasn't.



回答3:

Building off of what @erdostom wrote earlier (this was a lifesaver insight btw, thanks!) here's how I updated my AppDelegate.m

In the method supplied by the fbsdk, instead of returning 'handled',

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

  NSString *myUrl = url.absoluteString;
  if ([myUrl containsString:@"nameONonFBUrl"]) {
    return [RCTLinkingManager application:application
                                          openURL:url
                                sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                       annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
  } else {
    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                  openURL:url
                                                        sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                               annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
  }
}


回答4:

[FBSDKApplicationDelegate sharedInstance] and RCTLinkingManager both return a BOOL.

Because you can only declare the following method once in your AppDelegate:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

You can simply include them both within the same method, and return YES, if one of either [FBSDKApplicationDelegate sharedInstance] or RCTLinkingManager returns YES, like so:

Complete Method:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

  BOOL handleFBSDK = [[FBSDKApplicationDelegate sharedInstance] application:application
    openURL:url
    sourceApplication:sourceApplication
    annotation:annotation
  ];

  BOOL handleRCT = [RCTLinkingManager application:application 
    openURL:url 
    sourceApplication:sourceApplication 
    annotation:annotation
  ];

  return handleFBSDK || handleRCT;
}


回答5:

I need to follow this link to make it works https://github.com/facebook/react-native-fbsdk#32-ios-project . It said that AppDelegate.m file can only have one method for openUrl that is required in both of FacebookSDK and Deeplinking. There is also a solution to implement the function properly.