Facebook SDK login never calls back my application on iOS 9

IosObjective CFacebook

Ios Problem Overview


I've followed this guide to update my application to use Facebook SDK 4.6 to work properly when built with the iOS 9 SDK.

When I tap the login button now, a Safari view controller gets presented (shouldn't it redirect to the Facebook app?), but after accepting permission the Safari view controller is never dismissed. It loads a new blank page and sits there doing nothing. If I tap the Done button, the returned FBSDKLoginManagerLoginResult's isCancelled is true.

Is it normal that the SDK is choosing the Safari view controller over the Facebook app? And why am I not getting callbacks after login is complete?

Ios Solutions


Solution 1 - Ios

Turns out that on iOS 9 when UIApplicationDelegate's application:openURL:options: is implemented, application:openURL:sourceApplication:annotation: will not get called.

So what I had to do is call FBSDKApplicationDelegate's application:openURL:sourceApplication:annotation: from UIApplicationDelegate's application:openURL:options:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    return [[FBSDKApplicationDelegate sharedInstance] application:app
                                                      openURL:url
                                            sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                                   annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}

Solution 2 - Ios

For Swift this was working for me (add it in AppDelegate.swift):

@available(iOS 9.0, *)
func application(application: UIApplication,openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, 
     openURL: url, 
     sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String,
     annotation: options [UIApplicationOpenURLOptionsAnnotationKey])
}

and

@available(iOS, introduced=8.0, deprecated=9.0)
func application(application: UIApplication,openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {  
return FBSDKApplicationDelegate.sharedInstance().application(application,
     openURL: url,
     sourceApplication: sourceApplication!,
     annotation: annotation)
}

In each case remember to add import FBSDKCoreKit with the other import statements.

Its basically what Google SignIn uses. If its still not working you need to set the delegates and your info.plist like it is specified in the FaceBook Docs. I hope this helps!

Solution 3 - Ios

For Swift 3 & Facebook SDK 4.16.0:

Add the following code to AppDelegate.swift

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}

Solution 4 - Ios

For those of you experiencing this same issue with iOS10 I added this:

@available(iOS 9.0, *)
func application(_ application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}

This should work but as for now its just a workaround

Solution 5 - Ios

I have just come across this issue, thanks @Hesham for the fix.

Here is the Swift3 fix:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(
        app,
        open: url,
        sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String,
        annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}

Solution 6 - Ios

for xamarin users :

public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
        {

            OnUrlProtocolDetected(url.ToString());
            if (url.AbsoluteString.StartsWith("fb"))
            {
                return ApplicationDelegate.SharedInstance.OpenUrl(application, url, sourceApplication, annotation);
            }

}

Solution 7 - Ios

Did you follow these steps?

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

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

I think you are missing the point where you got to call the applications delegate in App delegate. The second method is vital coz it gives the callback to your application about the login did finish in safari by the user

Solution 8 - Ios

I was using the old 3.24 version and on iOS 9 I was facing a similar problem.

Found that in appDelegate method - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

was

return [FBSession.activeSession handleOpenURL:url];

instead of

return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];

Solution 9 - Ios

For total swift newbies like myself, the problem was an alternate implementation of the application() method:

Working version:

 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(
        application,
        open: url,
        sourceApplication: sourceApplication,
        annotation: annotation)
}

Non working version (Copied from somewhere )

func application(application: UIApplication, openURL url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(
        application,
        open: url,
        sourceApplication: sourceApplication,
        annotation: annotation)
}

Solution 10 - Ios

For anyone looking for Swift assistance on this very issue the following worked for me.

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: UIApplicationOpenURLOptionsSourceApplicationKey, annotation: UIApplicationOpenURLOptionsAnnotationKey)
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHeshamView Question on Stackoverflow
Solution 1 - IosHeshamView Answer on Stackoverflow
Solution 2 - IosDavidView Answer on Stackoverflow
Solution 3 - IosJT501View Answer on Stackoverflow
Solution 4 - IosCommittedEelView Answer on Stackoverflow
Solution 5 - IosRichAppzView Answer on Stackoverflow
Solution 6 - IosAbdullah TahanView Answer on Stackoverflow
Solution 7 - IosSaheb RoyView Answer on Stackoverflow
Solution 8 - IosJakub TruhlářView Answer on Stackoverflow
Solution 9 - IosvictorView Answer on Stackoverflow
Solution 10 - IosPearson BashamView Answer on Stackoverflow