我尝试在用户登录后捕获用户 Facebook 详细信息的结果,实例化 View Controller 并显示该 Controller 。
但是奇怪的是,当我的 getFBUserData()
方法中有 instantiateViewControllerWithIdentifier
代码时,它会在 FBSDKGraphRequest
之后退出。当我删除那 block 注释代码时,它完美地执行了变量的存储。
我还尝试将 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) 由于此方法 FBSDKGraphRequest
在后台线程上进行异步调用。您需要在主线程上执行 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)
});
关于ios - Facebook 登录后实例化ViewControllerWithIdentifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006908/