Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Facebook and Google login at the same time?

I'm trying to implement google+ and facebook sign ins in the same app. I followed the instructions by Parse and Google and I first successfully implemented Facebook login. Then I started to follow the google instructions. After doing that, now I'm having these 2 functions in my AppDelegate.swift file:

For google:

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

For facebook:

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

Do I need to combine these 2 blocks of code into 1 as seen in some posts such as

Google SignIn SFSafariViewController/WebView redirects to Google.com after accepting permissions

or they can stay like that in 2 different blocks? Thanks

like image 908
NewbieWantsToMaster Avatar asked Dec 24 '22 11:12

NewbieWantsToMaster


2 Answers

You need to combine them. The simplest way is to use an || on the return statement. Try this:

func application(application: UIApplication,
    openURL url: NSURL, options: [String: AnyObject]) -> Bool {

        return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: nil)  ||
            GIDSignIn.sharedInstance().handleURL(url,
            sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
            annotation: options[UIApplicationOpenURLOptionsAnnotationKey] as? String)
}
like image 102
lonesomewhistle Avatar answered Jan 11 '23 12:01

lonesomewhistle


You can also try this,

func application(_ application: UIApplication,
                 open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    if GIDSignIn.sharedInstance().handle(url) {
        return true
    } else if ApplicationDelegate.shared.application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) {
        return true
    }
    
    return false
}

@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
    if GIDSignIn.sharedInstance().handle(url) {
        return true
    } else if ApplicationDelegate.shared.application(app, open: url, sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplication.OpenURLOptionsKey.annotation]
        ) {
        return true
    }
    return false
}
like image 35
sadat Avatar answered Jan 11 '23 13:01

sadat