我正在使用示例程序来学习 facebook-ios-sdk,我发现了一个问题:如果我在我的设备上登录 Facebook 并运行该示例应用程序,一切都很好,但是,当我从设备中删除我的 Facebook 帐户并再次运行该示例应用程序,我仍然可以通过登录过程并且仍然可以看到 SCViewController(有一个快速应用程序切换到 Facebook 应用程序的过程,但我只需要单击“OKey”按钮,我没有无需填写任何电子邮件/密码信息即可登录 Facebook)。
我检查了代码,发现我的帐户从设备中删除后,FBSession.activeSession.accessToken
仍然认为里面的令牌是有效的。有什么问题吗?以及如何清除令牌并弹出登录对话框?我已经[FBSession.activeSession closeAndClearTokenInformation]
在注销时调用了,根据Facebook sdk文档,应该清除令牌,但事实并非如此。
我使用的环境:XCode 4.6.1、iPad 6.1 模拟器和 facebook-ios-sdk v3.2.1。
更新:在此处粘贴一些代码:在SCAppDelegate.m中,我添加了3个函数,不在示例代码中,而是在SDK在线文档中:
- (void)showLoginView
{
UIViewController* topViewController = [self.navigationController topViewController];
UIViewController* modalViewController = [topViewController modalViewController];
// If the login screen is not already displayed, display it. If the login screen is
// displayed, then getting back here means the login in progress did not successfully
// complete. In that case, notify the login view so it can update its UI appropriately.
if (![modalViewController isKindOfClass:[SCLoginViewController class]]) {
SCLoginViewController* loginViewController = [[SCLoginViewController alloc]
initWithNibName:@"SCLoginViewController"
bundle:nil];
[topViewController presentModalViewController:loginViewController animated:NO];
} else {
SCLoginViewController* loginViewController = (SCLoginViewController*)modalViewController;
[loginViewController loginFailed];
}
}
- (void)sessionStateChanged:(FBSession*)session state:(FBSessionState)state error:(NSError*)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController* topViewController = [self.navigationController topViewController];
if ([[topViewController modalViewController]isKindOfClass:[SCLoginViewController class]]) {
[topViewController dismissModalViewControllerAnimated:YES];
}
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[self.navigationController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
[FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession* session, FBSessionState status, NSError* error){
[self sessionStateChanged:session state:status error:error];}];
}
然后在 SCLoginViewController.m 中,我添加了一个按钮,而不是使用现有的 FBLoginView 对象来完成登录工作。该按钮的处理函数如下:
- (IBAction)performLogin:(id)sender {
//[self.spinner startAnimating];
SCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openSession];
}
然后在 SCViewController.m 中,我添加了另一个按钮来完成注销工作。该按钮的处理函数如下:
- (IBAction)logoutButtonWasPressed:(id)sender {
[FBSession.activeSession closeAndClearTokenInformation];
}
其他代码与示例代码中的几乎相同。