Google SingIn in Swift获取个人资料信息

时间:2015-11-20 17:47:36

标签: ios iphone swift google-login

我使用google + login在AppDelegate中获取用户个人资料信息。

如何在viewController中访问用户个人资料信息?

AppDelegate代码:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
    withError error: NSError!) {
        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 name = user.profile.name
            let email = user.profile.email
            // [START_EXCLUDE]

            print(userId)

        NSNotificationCenter.defaultCenter().postNotificationName(
                "ToggleAuthUINotification",
                object: nil,
                userInfo: ["statusText": "Signed in user:\n\(name)"])
            // [END_EXCLUDE]
        } else {
            print("\(error.localizedDescription)")
            // [START_EXCLUDE silent]
            NSNotificationCenter.defaultCenter().postNotificationName(
                "ToggleAuthUINotification", object: nil, userInfo: nil)
            // [END_EXCLUDE]
        }
}

1 个答案:

答案 0 :(得分:1)

I think you try this. you will get definite result 
remember to import some library

import AddressBook
import MediaPlayer
import AssetsLibrary
import CoreLocation
import CoreMotion
/
/
    //MARK: Google Login
        @IBAction func btnActionGoogleLogin(sender : UIButton)
        {
            var googleSignIn: GPPSignIn!
            googleSignIn = GPPSignIn.sharedInstance();
            googleSignIn.shouldFetchGooglePlusUser = true;
            googleSignIn.clientID = "";
            googleSignIn.shouldFetchGoogleUserEmail = true;
            googleSignIn.shouldFetchGoogleUserID = true;
            googleSignIn.scopes = [kGTLAuthScopePlusLogin];
            googleSignIn.scopes = ["profile"]
            googleSignIn.delegate = self;
            googleSignIn.authenticate();
        }

        //MARK: G+ Delegate
        func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
            let plusService :GTLServicePlus = GTLServicePlus.init()
            plusService.retryEnabled = true
            plusService.authorizer = GPPSignIn.sharedInstance().authentication
            plusService.apiVersion = "v1"

            let query : GTLQueryPlus = GTLQueryPlus.queryForPeopleGetWithUserId("me") as! GTLQueryPlus

            plusService.executeQuery(query) { (ticket,person, error) -> Void in
                if ((error) != nil) {
                    //Handle Error
                } else {
                    NSLog("Email= %@", GPPSignIn.sharedInstance().authentication.userEmail)
                    NSLog("GoogleID=%@", person.identifier)
                    print(person)
                    print(person.displayName)
                }
            }

        }
        func didDisconnectWithError(error: NSError!) {

        }
    }