Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppDelegate.m for both FBSDK and LinkingManager

To use FBSDK I need this snippet in 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;
}

To use LinkingManager I need this snippet in 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];
}

They're obviously duplicates. How do I combine the two so both libraries work? I don't know any Objective-C

like image 367
manni Avatar asked Aug 14 '16 19:08

manni


2 Answers

Of course, you can implement this method only once in your AppDelegate.

[[FBSDKApplicationDelegate... and [RCTLinkingManager... both return a BOOL.

You can put both snippets in the same method. I would suggest to return YES, if both [RCTLinkingManager... and [[FBSDKApplicationDelegate... return YES. Otherwise, return NO.

It could look like this:

- (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;
}

I can't guarantee, that you can use FBSDKApplicationDelegate and RCTLinkingManager in the same app, because I have never worked with this. But your code should at least compile.

like image 144
FelixSFD Avatar answered Nov 03 '22 01:11

FelixSFD


Wow, exactly what I was looking for! The accepted answer works great, except for a slight variation, since my FBSDK implementation is different (newer?). Because it uses:
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {

I just tried to use the same parameters as FBSDK, and it works!

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

Full method:

- (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

like image 3
Frexuz Avatar answered Nov 03 '22 01:11

Frexuz