Search code examples
iosfacebookreact-nativefbsdk

React Native FBSDK Login - Login with app fails


I'm implementing FB-login to my react-native app. FB-login is working when logging in with email/password in browser-window. But when choosing "Log in with facebook-app", I'm being directed to the fb-app (as it should. Everything fine so far). From here I click "Continue" (already logged in in fb-app), and then I'm redirected back to the facebook-dialogue which is opened in the app (still good). Here the facebook-dialogue closes automatically, as it should do, as I logged in using the app. But instead of it being a successful login, in the callback if(result.isCancelled) is triggered.

This only happens when using the facebook-app for login.

Login code:

LoginManager.logInWithReadPermissions(["email", 'public_profile']).then(function(result){
      if(result.isCancelled){
        console.log('Login cancelled');
      }
      else{
         // do login stuff.
      }
}, function(error){
  console.log("An error occured:");
  console.log(error);
});

AppDelegate.m

#import "AppDelegate.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <React/RCTLinkingManager.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [[FBSDKApplicationDelegate sharedInstance] application:application
                           didFinishLaunchingWithOptions:launchOptions];

  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"ntbscanpix"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f 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 {

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

  // Add any custom logic here.
  return handled;
}

- (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:(NSUserActivity *)userActivity
            restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {

  return [RCTLinkingManager application:application
                      continueUserActivity:userActivity
                      restorationHandler:restorationHandler];
}
@end

I see this error in appdelegate:

Conflicting parameter types in implementation of 'application:continueUserActivity:restorationHandler:': 'void (^ _Nonnull __strong)(NSArray<id<UIUserActivityRestoring>> * _Nullable __strong)' vs 'void (^__strong _Nonnull)(NSArray * _Nullable __strong)'

But I'm not able to fix that error. Could this be the reason it is failing logging in from app?

If not, anyone knows why logging in with the facebook-app causes cancelling of login?


Solution

  • I fixed it.

    The issue is in The AppDelegate.m. The file can only have one method for openUrl. It was two of them now, so I had to merge them. This worked:

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