2015-10-16 28 views
3

我已經安裝了Facebook的SDK,跟着所有的需求,它的作品,因爲我可以從用戶的Facebook帳戶中提取數據,不過,我可以不執行一次記錄在SEGUE。登錄與FBSDK,不能執行賽格瑞

我得到一個錯誤信息:

警告:Attempt to present <viewController1> on <FBSDKContainerViewController> whose view is not in the window hierarchy!

這裏是我的代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    if (FBSDKAccessToken.currentAccessToken() == nil) { 

     print("not logged in") 
    } 
    else { 

     print("logged in") 
    } 


    let loginButton = FBSDKLoginButton() 
    loginButton.readPermissions = ["public_profile", "email", "user_friends"] 
    loginButton.center = self.view.center 
    loginButton.delegate = self 
    self.view.addSubview(loginButton) 
} 

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


    if ((error) != nil) 
    { 

    } 
    else if result.isCancelled { 

    } 
    else { 

     if result.grantedPermissions.contains("email") 
     { 
      // Do work 

      returnUserData() 
      addViewHierarchy() 

      // self.performSegueWithIdentifier("signUpFollowPage", sender: self) 


     } 
    } 
} 

任何人都可以提供幫助嗎? 謝謝

+0

發佈顯示用FB登錄的代碼。 – Rumin

+0

我已添加相應的代碼。謝謝 – jim

+0

同樣的問題,沒有「美麗」的解決方案。對此有何建議? –

回答

7

我一直在努力解決同樣的問題(雖然我使用FBSDKLoginManagerlogInWithReadPermissions)。

雖然問題似乎是在Safari Web視圖關閉之前調用回調(或您的didCompleteWithResult委託調用),但我找不到一個好的解決方案。

我的(可怕的)答案一直是推遲繼續,給FBSDKContainerViewController時間自然被解僱。在你的情況下:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {() -> Void in 
    self.performSegueWithIdentifier("signUpFollowPage", sender: self) 
} 

它可能會逃脫一個較短的延遲。

這讓我覺得是Facebook登錄SDK的一個bug。如果我找到時間,我可以看看SDK source。幸運的是,我正在開發的應用程序不會在新的一年發佈,所以我希望無論如何這個問題可能會在更新的版本中得到修復。

+1

改進:檢查'self.view.window'是否爲'nil'。如果不是,您可以毫不拖延地執行這個過程。 – bevoy

0

我在使用FB登錄時遇到同樣的問題。這是我如何解決它。問題出在導航時,FBContainerView仍處於打開狀態。使用self.controller將引用FBContainerView而不是最上面的控制器,因此沒有導航。我們需要切換回頂部的大多數控制器,然後執行導航。

你可以做到這一點,而不執行SEGUE,通過從頂部控制器呈現視圖控制器這樣

let topController = self.topMostController() 
topController.presentViewController(secondController, animated: true, completion: nil) 

,以獲得最上面的控制器定義將始終返回最上面的控制器的方法

func topMostController()-> UIViewController { 
    var topController = UIApplication.sharedApplication().keyWindow?.rootViewController 

    while((topController?.presentedViewController) != nil){ 
     topController = topController?.presentedViewController 
    } 
    if(topController != nil){ 
     return topController! 
    } 
    else{ 
     return self 
    } 

} 
1

您應該使用viewDidAppear()函數並檢查FBSDKAccessToken.currentAccessToken

override func viewDidAppear(animated:Bool){ 
    if FBSDKAccessToken.currentAccessToken() != nil{ 
    performSegueWithIdentifier("YOUR_SEGUE",sender:self) 
    } 
}