Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling openURL: with Facebook and Google

user in my app can login using 2 services : Facebook or Google

everything works fine, however, in the :

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

i should decide to call the Facebook callback or Google callback

if the user has the apps, its easy, than i decide by the sourceApplication but when not (no native Facebook account linked in, no FB app, no GooglePlus app), it links to the browser :( and i dont know if it is comming from Facebook or Google

is there a way how to decide what to call? like

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation: (id)annotation {      // how to decide?     if (facebook) {          return [FBSession.activeSession handleOpenURL:url];      } else if (google) {          return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];      }  } 
like image 620
Peter Lapisu Avatar asked Jun 19 '13 18:06

Peter Lapisu


2 Answers

We don't need to Explicitly check the URL, the code below does it:

- (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation {      if ([GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation]) {         return YES;     }else if([FBAppCall handleOpenURL:url sourceApplication:sourceApplication]){         return YES;     }      return NO; } 
like image 190
user2144179 Avatar answered Sep 29 '22 20:09

user2144179


For Swift users, (The idea from user2144179)

Import below frameworks

import Firebase import GoogleSignIn import FacebookCore // (FBSDKCore's alternative for swift) 

and in your delegate methods

// when your target is under iOS 9.0 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {     let isFBOpenUrl = SDKApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)     let isGoogleOpenUrl = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)     if isFBOpenUrl { return true }     if isGoogleOpenUrl { return true }     return false } 

or you can use another 'open url:' method if your target is 9.0+. SDKs include a method for it also.

Firebase Reference : https://firebase.google.com/docs/auth/ios/google-signin

Facebook Reference : https://developers.facebook.com/docs/swift/reference/classes/applicationdelegate.html

like image 25
Mark Avatar answered Sep 29 '22 19:09

Mark