2015-11-20 48 views
1

我使用google + login獲取AppDelegate中的用戶個人資料信息。Google SingIn in Swift獲取個人資料信息

如何訪問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
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!) { 

     } 
    } 
相關問題