Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Using both Facebook and Google, Google Plus sign in

Good morning,

I'm trying to implement the Facebook login (which is working fine) and also the google plus login in the same view. I'm following the guides from the official site (Google) but there is an issue between the Facebook button and the google plus button:

Facebook tells me to put the following code:

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

    return [FBAppCall handleOpenURL:url
                  sourceApplication:sourceApplication];
}

And also Google tells me to put the following:

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

And I can't have the same functions with the same name and I can't integrate one inside the other. That's why I need your help because I don't know how to deal with this issue between these guidelines.

Thanks in advance.

like image 217
WirajPS Avatar asked Aug 19 '14 07:08

WirajPS


People also ask

Is it better to sign in with Google or Apple?

Using the option to sign in with Google is safe. Google's strong security and OAuth system provide better protection than current poor password practices. Users should understand the privacy concerns. Authenticators share data and account permissions to third-parties while collecting user login and traffic.

Is it safe to use sign in with Google?

Sign in with Google authenticates you uniquely for each service. So, even if there is a data breach on any of the sites or services you use, your personal information on all other accounts stays safe from hackers.

Should I sign in with Google or email?

But which service is best for secure accounts? Gmail, despite our warnings about Google accounts, is actually perfectly safe and secure — provided you don't “log in with Google” when prompted. Your email address should be just that: an email address. It should be used only as a username to sign in with.


2 Answers

Here you will have to check [url scheme] before returning. Example code is below.

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    NSLog("%@", [url scheme]); 

    if([[url scheme] isEqualToString:FACEBOOK_SCHEME]) 
    {  

        return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];

    }
    else if([[url scheme] isEqualToString:GOOGLE_PLUS_SCHEME]) 
    {  

        return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation]; 

    }
}

Hope it will solve your problem..

like image 75
Sreejith Bhatt Avatar answered Oct 13 '22 03:10

Sreejith Bhatt


Here is the version in Swift.

Firstly, you should understand how to see difference between Google and Facebook return url. Algorithm for detection is:

  1. Facebook URL pattern: fb1106884526040279://authorize/... with fb1106884526040279 is your app id. So you just need to check if this url start with your app id, is host equals to authorize (the string after // and first /)

  2. Google URL Pattern: com.googleusercontent.apps.803921065829-m34o26vcj57oirk1oa8pqve5o22qdihn So you just need to check if this url start with com.googleusercontent.apps.

Base on above explanation, I have generalized into GoogleAuth and FacebookAuth utils class with some small methods such as logout, check login ...

Here is Google Authentication Utils

import Google

class GoogleAuth {

    static func getInstance() -> GIDSignIn {
        return GIDSignIn.sharedInstance()
    }

    static func isLogin() -> Bool {
        return getInstance().hasAuthInKeychain()
    }

    static func signOut() {
        getInstance().signOut()
    }

    static func isValidatedWithUrl(url: NSURL) -> Bool {
        return url.scheme.hasPrefix(NSBundle.mainBundle().bundleIdentifier!) || url.scheme.hasPrefix("com.googleusercontent.apps.")
    }
}

Secondly is Facebook Authentication Utils

import FBSDKLoginKit

class FacebookAuth {
    static func isLogin() -> Bool {
       return FBSDKAccessToken.currentAccessToken() != nil
    }

    static func signOut() {
       FBSDKLoginManager().logOut()
    }

    static func isValidatedWithUrl(url: NSURL) -> Bool {
        return url.scheme.hasPrefix("fb\(FBSDKSettings.appID())") && url.host == "authorize"
    }
}

Here is the main part. In AppDelegate, you call isValidateWithUrl method for checking if this url is from Google or Facebook.

func application(application: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {
        print("step 2 of OAuth2. Url: \(url)")

        // url from google
        if GoogleAuth.isValidatedWithUrl(url) {
            return GIDSignIn.sharedInstance().handleURL(
                    url,
                    sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
                    annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
        }
        // url from facebook
         else if FacebookAuth.isValidatedWithUrl(url) {
             return FBSDKApplicationDelegate.sharedInstance().application(application,
                    openURL: url,
                    sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String,
                    annotation: options [UIApplicationOpenURLOptionsAnnotationKey])
        }
        // application hasn't supported this url yet
        else {
              return false
        }
    }

Hope this help :)

like image 30
hqt Avatar answered Oct 13 '22 05:10

hqt