2013-01-21 54 views
10

如何關閉會話Facebook-iOS-Sdk每當我啓動我的應用程序或殺死應用程序後,它會給我以前的登錄會話詳細信息。當我點擊facebook按鈕。我已經使用了所有的東西[appDelegate.session closeAndClearTokenInformation]; [FBSession.activeSession close];[FBSession.activeSession closeAndClearTokenInformation] ..但活動會話仍然沒有關閉..如何關閉點擊前的會話數據。關閉Facebook會話在Facebook-Ios-Sdk

我使用下面的代碼來清除,但每當我點擊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]; 
}  


} 

回答

28

喜Christien可能是波紋管兩者結合起來,其幫助的請你refur的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

希望它可以幫助你

+0

你好謝謝你!我已經嘗試了所有的事情,但這個奇怪的事情一次又一次地發生:(...... – Christien

+1

這確實很奇怪與fb api。我有同樣的「問題」。順便說一句,當你從facebook應用程序註銷(實際fb應用程序你的設備),那麼你真的註銷了,如果你只註銷你的應用程序(而不是fb的),你將用sso登錄你在fb應用程序中使用的帳戶... – Yanchi

+1

太棒了!謝謝!上面的工作完全適合我。 – Anthony

4

調用

[FBSession.activeSession closeAndClearTokenInformation]; 

使用以下後清除 餅乾。它爲我工作!

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

不適用於我。 – keen

+2

當我點擊登錄按鈕時沒有清除舊會話,它總是會讓我看到「您已經授權了應用程序的名稱」屏幕 –

+0

@SonnyG是否能夠找出解決方案? – Devarshi

0

尼廷的Gohel答案,斯威夫特版本:

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

可以使用

FBSession.setActiveSession(nil) 

但功能要求展開的對象,所以 「無」 似乎不是一個好參數通過