2016-01-26 68 views
0

我試圖在登錄後捕獲用戶Facebook詳細信息的結果,實例化視圖控制器並顯示控制器。在Facebook登錄後instantiateViewControllerWithIdentifier

然而奇怪的是,當我有我的getFBUserData()方法內instantiateViewControllerWithIdentifier代碼,它FBSDKGraphRequest後退出。當我刪除那塊評論代碼時,它會完美地執行變量的存儲。

我也試過在self.getFBUserData()之後放置instantiateViewControllerWithIdentifier代碼,並且它也不再存儲變量。

我需要幫助存儲變量,然後執行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 
      } 
     }) 
    } 

} 

回答

0

1)由於上回地面這種方法FBSDKGraphRequest化妝asych呼叫thread.You需要在主線程上的用戶界面相關的工作。

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) 
    }); 
+0

唉唉確定...以防萬一的誰面臨着同樣的問題的人,我把這個代碼的大括號後'self.fbProfileImage =的UIImage(數據:NSData的(contentsOfURL:NSURL(字符串:strPictureURL) !)!)!' – jo3birdtalk