在Facebook-Ios-Sdk关闭Facebook会话

时间:2013-01-21 04:56:23

标签: iphone ios facebook facebook-graph-api

如何在每次启动我的应用程序后关闭a Facebook-iOS-Sdk一个特定会话{@ 1}}在杀死应用程序之后它会给我之前的登录会话详细信息。当我点击facebook按钮时。我已经使用了所有内容[appDelegate.session closeAndClearTokenInformation];       [FBSession.activeSession close]; [FBSession.activeSession closeAndClearTokenInformation] ..但是活动会话仍然没有结束。我如何在点击时关闭以前的会话数据。

我使用下面的代码清除,但每当我点击Facebook按钮开始新的会话时..它会返回以前的凭据。

- (IBAction)buttonClickHandler:(id)sender {

appDelegate = [[UIApplication sharedApplication]delegate];

// this button's job is to flip-flop the session from open to closed
if (appDelegate.session.isOpen) {

    [appDelegate.session closeAndClearTokenInformation];
    [FBSession.activeSession close];
    [FBSession.activeSession  closeAndClearTokenInformation];
    FBSession.activeSession=nil;

} else {
    if (appDelegate.session.state != FBSessionStateCreated) {
        // Create a new, logged out session.
        appDelegate.session = [[FBSession alloc] init];
    }

    // if the session isn't open, let's open it now and present the login UX to the user
    [FBSession.activeSession closeAndClearTokenInformation];
    FBSession.activeSession=nil;
    [FBSession.activeSession openWithBehavior:FBSessionLoginBehaviorForcingWebView

                            completionHandler:^(FBSession *session,

                                                FBSessionState state,

                                                NSError *error) {
                                // and here we make sure to update our UX according to the new session state

                                NSLog(@" state=%d",state);

                                [FBRequestConnection
                                 startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                                   id<FBGraphUser> user,
                                                                   NSError *error) {

                                     userInfo = @"";

                                     // Example: typed access (name)
                                     // - no special permissions required
                                     userInfo = user.username;

                                     NSLog(@"string %@", userInfo); 
                                          [self checkfacebook];

                                 }];


                            }];    } 


 }

并在 ViewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];





AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
if (!appDelegate.session.isOpen) {
    // create a fresh session object
    appDelegate.session = [[FBSession alloc] init];


    if (appDelegate.session.state == FBSessionStateCreatedTokenLoaded) {
        // even though we had a cached token, we need to login to make the session usable
        [FBSession.activeSession close];
        FBSession.activeSession=nil;
   }
}



NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:@"http://login.facebook.com"]];

for (NSHTTPCookie* cookie in facebookCookies) {
    [cookies deleteCookie:cookie];
}    


}

3 个答案:

答案 0 :(得分:28)

嗨,Christien可能会在下面提供两个链接,请你重新提出developers.facebook

这两个链接

http://developers.facebook.com/docs/reference/ios/3.0/class/FBSession/

http://developers.facebook.com/docs/reference/ios/3.0/class/FBSession/#close

[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];

您还可以查看与您的问题相关的这两个问题: -

Facebook graph api log out not working

Facebook iOS SDK 3.1.1 "closeAndClearTokenInformation" method no working

希望它可以帮助你

答案 1 :(得分:4)

致电后

    [FBSession.activeSession closeAndClearTokenInformation];

使用以下清除Cookie。它对我有用!

     NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie* cookie in
     [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
    [cookies deleteCookie:cookie];
}

答案 2 :(得分:0)

Nitin的Gohel回答,Swift版本:

    FBSession.activeSession().closeAndClearTokenInformation()
    FBSession.activeSession().close()
    FBSession.setActiveSession(FBSession())

你可以使用

    FBSession.setActiveSession(nil)

但是该函数要求一个未包装的对象,所以&#34; nil&#34;似乎不是一个好的参数传递