适用于FBSDK和LinkingManager的AppDelegate.m

时间:2016-08-14 19:23:20

标签: ios objective-c react-native

要使用FBSDK,我需要在app delegate

中使用此代码段
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

  BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
    openURL:url
    sourceApplication:sourceApplication
    annotation:annotation
  ];
  // Add any custom logic here.
  return handled;
}

要使用LinkingManager,我需要在app delegate

中使用此代码段
#import "RCTLinkingManager.h"

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

他们显然是重复的。我如何将两者结合起来使两个库都有效? 我不知道任何Objective-C

3 个答案:

答案 0 :(得分:12)

当然,您只能在AppDelegate

中实施此方法一次

[[FBSDKApplicationDelegate...[RCTLinkingManager...都返回BOOL

您可以将两个代码段放在同一个方法中。如果YES[RCTLinkingManager...都返回[[FBSDKApplicationDelegate...,我建议您返回YES。否则,请返回NO

看起来像这样:

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

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

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

  return handledFB || handledRCT;
}

我无法保证,您可以在同一个应用中使用FBSDKApplicationDelegateRCTLinkingManager,因为我从未使用过此功能。但是你的代码至少应该编译。

答案 1 :(得分:0)

哇,正是我想要的!可以接受的答案非常有用,除了略有不同之外,因为我的FBSDK实现是不同的(新的?)。因为它使用:
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

我只是尝试使用与FBSDK相同的参数,所以它有效!

    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
    annotation:options[UIApplicationOpenURLOptionsAnnotationKey]

完整方法:

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

  BOOL handledFB = [[FBSDKApplicationDelegate sharedInstance] application:application
    openURL:url
    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
    annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
  ];

  BOOL handledRCT = [RCTLinkingManager application:application
    openURL:url
    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
    annotation:options[UIApplicationOpenURLOptionsAnnotationKey]
  ];

  return handledFB || handledRCT;
}

RN 0.59.x

答案 2 :(得分:0)

对于那些正在使用RNFirebase链接的人:

- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  
  BOOL handledFB = [[FBSDKApplicationDelegate sharedInstance]
                    application:application
                    openURL:url
                    sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                    annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
  
  BOOL handledRCT = [RCTLinkingManager application:application openURL:url options:options];

  if (!handledRCT) {
      handledRCT = [[RNFirebaseLinks instance] application:application openURL:url options:options];
  }

  return handledFB || handledRCT;
}