finishedWithAuth(auth:GTMOAuth2Authentication!,错误:NSError!)只调用一次

时间:2015-09-30 11:03:06

标签: ios objective-c swift google-plus ios9

我使用以下代码在我的ios app中实现facebook和google plus登录.Facebook工作正常。点击谷歌加登录按钮我可以通过safari或谷歌+应用程序输入详细信息。问题是第一次成功登录。当我点击其他视图控制器上的注销或断开按钮时,我没有再次登录。第二次不调用finishedWithAuth委托。虽然我再次提出了输入细节的观点。

请帮帮我。我已经搜遍了所有但没有希望。

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit


class ViewController:   UIViewController,FBSDKLoginButtonDelegate,GPPSignInDelegate {

var signIn = GPPSignIn.sharedInstance()
// @IBOutlet weak var signInButton: GPPSignInButton!

override func viewDidLoad() {
    super.viewDidLoad()


    signIn.shouldFetchGooglePlusUser = true
    signIn.shouldFetchGoogleUserEmail = true
    signIn.shouldFetchGoogleUserID = true

    signIn.scopes.append(kGTLAuthScopePlusLogin)
    signIn.scopes.append("profile")
    signIn.delegate = self
    signIn.trySilentAuthentication()

    if FBSDKAccessToken.currentAccessToken() == nil {

        print("Not logged in")

    } else {

        print("Logged in")

    }
  }

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//        if segue.identifier == "idSegueContent" {
//            contentViewController = segue.destinationViewController as! ContentViewController
//        }
}


 func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
    print("\(error), \(auth)")

    if ((error) != nil) {

        print(error.localizedDescription)

    } else {


        print(signIn.userEmail)
        print(signIn.userID)
        self.performSegueWithIdentifier("loggedIn", sender: self)

    }

}

func loginButton(fbLogin: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    if error == nil {
        print("logged")
        self.performSegueWithIdentifier("loggedIn", sender: self)

    }else {

        print(error.localizedDescription)
    }
}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
    print("logged out")
}

// Action for facebook login button
@IBAction func fbLoginAction(sender: AnyObject) {

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
        let fbloginresult : FBSDKLoginManagerLoginResult = result
        if(fbloginresult.grantedPermissions.contains("email"))
        {
            self.getFBUserData()

//                fbLoginManager.logOut()
        }
    }

}

// function to get data from facebook login
func getFBUserData(){
    if((FBSDKAccessToken.currentAccessToken()) != nil){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
            if (error == nil){
                let imageUrlDic: NSDictionary = (result.objectForKey("picture"))?.objectForKey("data") as! NSDictionary
                let imageUrl = imageUrlDic.objectForKey("url") as! String
                SingletonClass.sharedInstance.name = result.objectForKey("first_name") as! String
                SingletonClass.sharedInstance.userImage = imageUrl
//                    print(result)
                self.performSegueWithIdentifier("loggedIn", sender: self)
            }
        })
    }
}


@IBAction func googlePlusLogin(sender: AnyObject) {

    signIn.authenticate();

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
//         Dispose of any resources that can be recreated.
}

}

0 个答案:

没有答案