Facebook登录后,实例化ViewControllerWithIdentifier

时间:2016-01-26 04:08:24

标签: ios swift facebook

我尝试在登录后捕获用户Facebook详细信息的结果,实例化视图控制器并显示控制器。

然而奇怪的是,当我的instantiateViewControllerWithIdentifier方法中包含getFBUserData()代码时,它会在FBSDKGraphRequest之后退出。当我删除那段注释代码时,它会完美地执行变量的存储。

我还尝试在instantiateViewControllerWithIdentifier之后放置self.getFBUserData()代码,并且它也不会再次存储变量。

我需要帮助存储变量,然后执行显示存储变量的instantiateViewControllerWithIdentifier

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

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

                }
            }
            return;
        }
        else {
            let alertController = UIAlertController(title: "Limited Connectivity", message: "We're unable to log you in. Please check your network connectivity.", preferredStyle: .Alert)
            let action1 = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default){
                UIAlertAction in
            }

            alertController.addAction(action1)
            self.presentViewController(alertController, animated: true, completion: nil)

            fbLoginManager.logOut()
            return;
        }
    })
}

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){
                self.strName = (result.objectForKey("name") as? String)!
                self.strID = (result.objectForKey("id") as? String)!
                self.strEmail = (result.objectForKey("email") as? String)!
                let strPictureURL: String = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!
                self.fbProfileImage = UIImage(data: NSData(contentsOfURL: NSURL(string: strPictureURL)!)!)!

                /*
                let displayViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ChooseNameViewController") as! ChooseNameViewController

                displayViewController.email = self.strEmail
                displayViewController.id = self.strID
                displayViewController.name = self.strName
                displayViewController.profilePic = self.fbProfileImage

                self.navigationController?.pushViewController(displayViewController, animated: true)
                */
            }
            else{
                let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
                fbLoginManager.logOut()
                return
            }
        })
    }

}

1 个答案:

答案 0 :(得分:0)

1)由于此方法FBSDKGraphRequest在后​​台线程上进行asych调用。您需要在主线程上进行与UI相关的工作。

dispatch_async(dispatch_get_main_queue(), ^{
                let displayViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ChooseNameViewController") as! ChooseNameViewController
                displayViewController.email = self.strEmail
                displayViewController.id = self.strID
                displayViewController.name = self.strName
                displayViewController.profilePic = self.fbProfileImage

                self.navigationController?.pushViewController(displayViewController, animated: true)
    });