我正在使用Facebook SDK实现OAuth登录,但是FBSession.openActiveSessionWithReadPermissions:allowLoginUI:completionHandler方法没有调用completionHandler,也没有返回“true”,这表明缓存了令牌。除此之外,其他的都似乎很好,只有这里出了问题。这段代码有什么问题吗?
var completionHandler: FBSessionStateHandler = {
session, status, error in
NSLog("token not cached");
if error {
// Login failed for some reason, so we notify the delegate.
self.delegate.didFailOpenningSession?(self, withError: ErrorUtility.error(
code: .FailedOpenningSession,
withFacebookError: error
));
// Don't proceed on errors
return;
}
// Session is now open, we should notify the delegate
self.delegate.didOpenSession?(self);
};
NSLog("here");
// Open the session, prompting the user if necessary. This might send the application to the background
if FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: true, completionHandler: completionHandler) {
NSLog("Token cached");
// If we end up here, the session token must exist, so we now have an open session
self.delegate.didOpenSession?(self);
}
编辑:我忘记提到NSLog记录了“here”,但没有记录“Token cached”和“token not cached”
编辑:现在情况有点奇怪。当我的应用程序启动时,我会显示一个带有Facebook登录按钮的屏幕。当用户按下按钮时,上面的代码会被触发。在我授权该应用程序之后,应用程序返回到相同的屏幕(它不应该这样,但在完成处理程序被调用之前,我无法以其他方式完成)。然后我尝试再次按下按钮,现在我确实得到了日志。
这是我得到的:
here
token not cached
here
第一行来自第一次按下按钮,其他两行出现在我第二次按下按钮时。这有什么奇怪的地方吗?"token not cached"和"here"不应该反过来吗?它是从最后一次调用中调用completionHandler吗?
算了,这个问题已经解决了。
另外一个问题。如果我多次按下按钮,我总是得到"token not cached"。我认为它应该被缓存。