FBSDKAccessToken currentAccessToken nil after quitting app
我看了很多相关的线程,我的案例中的解决方案仍然难以辨认。
这是我的 AppDelegate.swift 文件:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
if let accessToken = FBSDKAccessToken.currentAccessToken(){
print(accessToken)
}else{
print("Not logged In.")
}
return true
}
func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
func application(application: UIApplication, openURL url:NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url,sourceApplication: sourceApplication, annotation: annotation)
}
所以在应用程序启动时 accessToken
是 nil
。
当我尝试刷新访问 token 时,它不会这样做,因为您无法引用一个无用户访问 token 。我想它可能会缓存一个过期的用户 token 。这仍然很奇怪,因为我正在调用应该返回当前用户 token 的 FBSDK,即使它已过期。在我的例子中,它返回 nil
。
下面是我如何尝试使用我的 currentAccessToken
func fetchFBInfoForLogin(){
let parameters = ["fields": "email, first_name, last_name"]
if (FBSDKAccessToken.currentAccessToken() != nil) {
print("current access token is not nil")
// User is logged in, do work such as go to next view controller.
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print(result.valueForKey("id") as? String)
print(result.valueForKey("name") as? String)
print(result.valueForKey("email") as? String)
}
})
} else {
//refresh
FBSDKAccessToken.refreshCurrentAccessToken{ (connection, result, error) -> Void in
if error != nil {
print(error)
return
}
print(FBSDKAccessToken.currentAccessToken())
print("refresh doesn't work?")
let parameters = ["fields": "email, first_name, last_name"]
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
print(result.valueForKey("id") as? String)
print(result.valueForKey("name") as? String)
print(result.valueForKey("email") as? String)
}
})
}
}
}
供将来引用:
我没有将我的 FBSDK 委托(delegate)调用放在正确的 AppDelegate
方法中,就像这篇 SO 帖子:Facebook SDK login never calls back my application on iOS 9
因此我不得不手动关闭我的 FB 登录,这导致回调的结果为 result.isCancelled
,因此我没有获得任何用户 token 。
最佳答案
如果您想要来自登录和注销事件的通知,请实现 FBSDKLoginButtonDelegate协议(protocol)。
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FBSDKLoginButtonDelegate {
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if ((error) != nil) {
// Process error
} else if result.isCancelled {
// Handle cancellations
} else {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, email, name"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil) {
//print("Error: \(error)")
} else {
if let user : NSString = result!.valueForKey("name") as? NSString {
print("user: \(user)")
}
if let id : NSString = result!.valueForKey("id") as? NSString {
print("id: \(id)")
}
if let email : NSString = result!.value(forKey: "email") as? NSString {
print("email: \(email)")
}
}
})
}
}
}
HTH.
关于ios9 : FBSDK currentAccessToken nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39188756/