Last active
April 5, 2017 06:25
-
-
Save randhirraj3130/5d5dcabb6a2bef0d7ac310ad4ee947dd to your computer and use it in GitHub Desktop.
how to apply google login in my app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// call the delegate on button click | |
// call following delegate into your view controller | |
//GIDSignInUIDelegate,GIDSignInDelegate | |
// register your app and download the googleservices-info.plist | |
GIDSignIn.sharedInstance().uiDelegate = self | |
GIDSignIn.sharedInstance().clientID = "58108526797-sen1e9fq0gnlicnkhb6krmflva86ie1k.apps.googleusercontent.com" | |
GIDSignIn.sharedInstance().signIn() | |
// call delegate function | |
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { | |
if (error == nil) { | |
// Perform any operations on signed in user here. | |
let userId = user.userID! // For client-side use only! | |
let idToken = user.authentication.idToken // Safe to send to the server | |
let fullName = user.profile.name! | |
let givenName = user.profile.givenName | |
let familyName = user.profile.familyName | |
let email = user.profile.email! | |
print("userId\(userId)") | |
print("idToken\(idToken)") | |
print("fullName\(fullName)") | |
print("\(givenName)") | |
print("\(familyName)") | |
print("email\(email)") | |
} else { | |
print("\(error.localizedDescription)") | |
} | |
} | |
func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) { | |
} | |
// initiate value on viewdidload | |
GIDSignIn.sharedInstance().uiDelegate = self | |
GIDSignIn.sharedInstance().clientID = "58108526797-sen1e9fq0gnlicnkhb6krmflva86ie1k.apps.googleusercontent.com" | |
GIDSignIn.sharedInstance().delegate = self | |
// call the following on app delegate .swift | |
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { | |
let handled_google = GIDSignIn.sharedInstance().handle(url,sourceApplication: sourceApplication,annotation: annotation) | |
return handled_google | |
} | |
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { | |
if #available(iOS 9.0, *) { | |
return GIDSignIn.sharedInstance().handle(url,sourceApplication: options [UIApplicationOpenURLOptionsKey.sourceApplication] as? String, | |
annotation: options[UIApplicationOpenURLOptionsKey.annotation]) | |
} else { | |
return false | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment