Wednesday, April 27, 2016

Google Sign-In for iOS - Get User Name, Email and Profile Picture without Nofitication

Previously, I have worked out how to implement a Google Sign-In button and display the user email and profile picture in these posts:

Google Sign-In for iOS - Create a GIDSignInButton programmatically in Swift
Google Sign-In for iOS - Display User Email and Profile Picture

When the user is just signed in with the two examples above, AppDelegate.swift posts a notification including the user information and then ViewController.swift receives the user information in the receiveToggleAuthUINotification function.

However, I need to separate the sign-in button and the display of the user information in two different view controllers. The view controller to show the user information is displayed AFTER signed in and hence the notification is unable to be detected. The solution is to include the code below:

if (GIDSignIn.sharedInstance().hasAuthInKeychain()) {
    print("signed in")
    print(GIDSignIn.sharedInstance().currentUser.profile.email)
    print(GIDSignIn.sharedInstance().currentUser.profile.name)
    print(GIDSignIn.sharedInstance().currentUser.profile.imageURLWithDimension(100))
}

Related Information:

Google Sign-In for iOS - Create a GIDSignInButton programmatically in Swift
Google Sign-In for iOS - Display User Email and Profile Picture

No comments:

Post a Comment