2016-11-22 43 views
6

我正在使用函數FBSDKAccessToken.currentAccessToken()來檢索FB令牌,該FB令牌工作正常,直到我在swift 3中遷移了我的應用程序, 4.17 SDK。 現在,該函數已重命名爲FBSDKAccessToken.current(),並且在應用程序委託重新加載時爲零。 我已經執行了一些測試,並且在重新啓動我的應用後我設法獲得了令牌,並且我之前已經登錄過FB,但那不是我期望的行爲。無法在swift 3/FBSDK中正確檢索FBSDKAccessToken當前令牌4.17

編輯:我恢復到4.15,它又重新工作。

這裏是我的AppDelegate代碼:

func applicationDidBecomeActive(_ application: UIApplication) 
{ 

    FBSDKAppEvents.activateApp(); 

} 

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> (Bool) 
{ 

    let wasHandled: Bool = FBSDKApplicationDelegate.sharedInstance().application(application 
     ,open:url 
     ,sourceApplication:sourceApplication 
     ,annotation:annotation) 

    if (wasHandled) { 
     if let fbtoken = FBSDKAccessToken.current() { 
      loginWithFacebook(fbtoken.tokenString); 
     }else{ 
      print("no current token") 
     } 
    } 

    return wasHandled 
} 

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 


    let wasHandled: Bool = FBSDKApplicationDelegate.sharedInstance().application(
     app, 
     open: url as URL!, 
     sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String, 
     annotation: options[UIApplicationOpenURLOptionsKey.annotation] 
    ) 

    if (wasHandled) { 
     if let fbtoken = FBSDKAccessToken.current() { 
      loginWithFacebook(fbtoken.tokenString); 
     }else{ 
      print("no current token") 
     } 
    } 

    return wasHandled; 
} 

感謝您的幫助:) 丹尼斯

+0

我也回滾與 莢 'FBSDKCoreKit', '4.15.0' 莢 'FBSDKLoginKit', '4.15.0' 莢 'FBSDKShareKit', '4.15.0' 和遵循HTTP://計算器。 com/a/41119777/1220633 現在可以在真實設備和模擬器上運行。 – RikiRiocma

回答

2

出於某種原因,這是不工作了。

如果您使用的是FBSDKLoginButton,則必須使用FBSDKLoginButtonDelegate來等待回調。

class LoginView: UIViewController, FBSDKLoginButtonDelegate { 
    @IBOutlet weak var facebookButton: FBSDKLoginButton! 

    override func viewDidLoad() { 
     facebookButton.delegate = self 
    } 

    //Called at the end of the user connection 
    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { 
     if ((error) != nil) { 
      //Process error 
     } else if result.isCancelled { 
      // Handle cancellations 
      print("result is canceled") 
     } else { 
      if let fbToken = result.token.tokenString { 
       print(fbToken) 
      } 
     } 
    } 

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) { 
     print("logout from FB") 
    } 
} 

或者,您也可以使用FBSDKLoginManager。這裏退房documation:

https://developers.facebook.com/docs/reference/ios/current/class/FBSDKLoginManager/

希望這有助於!