Handling openURL: with Facebook and Google
The method "application:openURL:sourceApplication:annotation:" is deprecated from iOS9. so now you can use like.
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary *)options {
// For Google sign in SDK
if ([[GIDSignIn sharedInstance] handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]) {
return YES;
// For Facebook SDK
}else if ( [[FBSDKApplicationDelegate sharedInstance] application:app
openURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]){
return YES;
//For Google plus SDK
}else if ([GPPURLHandler handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]){
return YES;
}
return NO;
}
You can Try the following :
if ([[url absoluteString] rangeOfString:@"<Your Google Bundle ID>"].location == NSNotFound)
{
// Call FBAppCall's handleOpenURL:sourceApplication to handle Facebook app responses
BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
// You can add your app-specific url handling code here if needed
return wasHandled;
}
else
{
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
return YES;
Call the above method in
(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
in your appDelegeate.m
Basically what this is going to do is examine the url prefix and then call the google+ method if url prefix is ur google+ bundle id , and if not , it'll call the fb method . Hope this helps
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
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;
}