首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用facebook-ios-sdk v3.2.1清除token?

如何使用facebook-ios-sdk v3.2.1清除token?
EN

Stack Overflow用户
提问于 2013-04-05 08:39:47
回答 1查看 3.6K关注 0票数 1

我正在使用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个函数,这3个函数不在示例代码中,而是在SDK在线文档中:

代码语言:javascript
复制
- (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对象。该按钮的处理函数如下所示:

代码语言:javascript
复制
- (IBAction)performLogin:(id)sender {
    //[self.spinner startAnimating];
    SCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate openSession];
}

然后在SCViewController.m中,我添加了另一个按钮来执行注销工作。该按钮的处理函数如下所示:

代码语言:javascript
复制
- (IBAction)logoutButtonWasPressed:(id)sender {
    [FBSession.activeSession closeAndClearTokenInformation];
}

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

EN

回答 1

Stack Overflow用户

发布于 2013-08-24 04:45:48

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

下面是我在iOS 5.1+上使用Facebook SDK 3+的方法:

代码语言:javascript
复制
[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];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15824101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档