如何使用facebook-ios-sdk v3.2.1清除令牌?

时间:2013-04-05 00:39:47

标签: ios facebook token facebook-ios-sdk

我正在使用sample program来学习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];
}

其他代码与示例代码中的代码几乎相同。

1 个答案:

答案 0 :(得分:0)

除了清除令牌外,您还需要在使用它登录Facebook时清除存储在Safari中的cookie。

以下适用于iOS 5.1 +上的Facebook SDK 3+:

[FBSession.activeSession closeAndClearTokenInformation];

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie *cookie in [storage cookies])
{
    NSString *domainName = [cookie domain];
    NSRange domainRange = [domainName rangeOfString:@"facebook"];
    if(domainRange.length > 0)
    {
        [storage deleteCookie:cookie];
    }
}