ios - Facebook 登录后实例化ViewControllerWithIdentifier

标签 ios swift facebook

我尝试在用户登录后捕获用户 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/

相关文章:

ios - 无法播放下载内容的 URL 中的视频

ios - Firebase 推送通知

swift - 即使我使用 .randomElement() 方法 (Swift) 2019/2020,每日提醒(通知)也不会发送随机消息

ios - ContentInset 在 popViewController 之后自动改变

ios - NSMutableArray 不在图表上显示数据

ios - 手动调用 didSelectRowatIndexPath

ios - 在一个 UIViewController 中设置 3 个 UICollectionView

javascript - 在 codeigniter 中使用 facebook 登录

android - 清空 Facebook FriendPickerFragment

javascript - 使用 Facebook 相册图 API 在照片弹出窗口中需要标题