在ios中使用Facebook SDK注销function

我使用Facebook SDK登录了facebook.But,再次转到相同的帐户,它在表格视图中显示两次值(For Ex:Prem Kumar.this名称在Cell中显示两次)。

//登录

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView { // first get the buttons set for login mode NSLog(@"success"); [HUD showWhileExecuting:@selector(LoadingProcess) onTarget:self withObject:nil animated:YES]; if (FBSession.activeSession.isOpen) { NSLog(@"TOKEN : %@",[[FBSession activeSession]accessTokenData]); FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,picture,birthday,location"]; [friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { NSArray *data = [result objectForKey:@"data"]; for (FBGraphObject *friend in data) { [delegate.friendsListArray addObject:friend]; NSLog(@"%@:%@:%@", [friend name],[friend birthday],[friend id]); } if ([delegate.friendsListArray count]!=0) { NSUserDefaults * standardDefaults=[NSUserDefaults standardUserDefaults]; [[NSUserDefaults standardUserDefaults] synchronize]; [standardDefaults setObject:delegate.friendsListArray forKey:@"FriendsListArray"]; [standardDefaults setBool:YES forKey:@"logged_in"]; [standardDefaults synchronize]; NSLog(@"%@",[standardDefaults objectForKey:@"FriendsListArray"]); BirthdayList * birthdaylist=[[BirthdayList alloc]init]; [self.navigationController pushViewController:birthdaylist animated:NO]; [birthdaylist release]; } else { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Friends not found" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; [alertView release]; } }]; } } 

在Settings.m中

 -(void)LogoutTapped { NSLog(@"Logged out of facebook"); NSUserDefaults * standardDefaults=[NSUserDefaults standardUserDefaults]; [[NSUserDefaults standardUserDefaults] synchronize]; NSArray *keys = [[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]allKeys]copy]; for(NSString *key in keys) { [standardDefaults removeObjectForKey:key]; [standardDefaults removeObjectForKey:@"FriendsListArray"]; [standardDefaults removeObjectForKey:@"logged_in"]; [standardDefaults synchronize]; NSLog(@"Key Name: %@", key); } [delegate.friendsListArray removeAllObjects]; [keys release]; NSLog(@"%@",[standardDefaults objectForKey:@"FriendsListArray"]); NSLog(@"%@",[standardDefaults objectForKey:@"logged_in"]); NSUserDefaults * userinfodefaults=[NSUserDefaults standardUserDefaults]; NSArray *userkeys = [[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]allKeys]copy]; for(NSString *userkey in userkeys) { [userinfodefaults removeObjectForKey:userkey]; [userinfodefaults removeObjectForKey:@"userinfo"]; [userinfodefaults removeObjectForKey:@"userbool"]; NSLog(@"key1 Name: %@",userkey); } [delegate.UserListArray removeAllObjects]; [delegate.Setmonthdataarray removeAllObjects]; [delegate.SetMonthlistarray removeAllObjects]; [userinfodefaults synchronize]; [userkeys release]; FBSession *session=[FBSession activeSession]; [session closeAndClearTokenInformation]; [session close]; [[FBSession activeSession] closeAndClearTokenInformation]; [[FBSession activeSession] close]; [FBSession setActiveSession:nil]; [delegate facebook].accessToken=nil; [delegate facebook].expirationDate=nil; NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage]; NSArray* facebookCookies = [cookies cookiesForURL: [NSURL URLWithString:@"http://login.facebook.com"]]; for (NSHTTPCookie* cookie in facebookCookies) { [cookies deleteCookie:cookie]; } for (NSHTTPCookie *_cookie in cookies.cookies) { NSRange domainRange = [[_cookie domain] rangeOfString:@"facebook"]; if(domainRange.length > 0){ [cookies deleteCookie:_cookie]; } } UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Logout" message:@"Logout Successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; MainList * mainlist=[[MainList alloc]initWithNibName:@"MainList" bundle:nil]; [self.navigationController pushViewController:mainlist animated:NO]; [mainlist release]; } 

还有一个疑问。

要登录Facebook,我使用了Facebook SDK。

要登出Facebook,我使用Facebook库。(Facebook.m,Facebook.h)

这有什么问题吗?

任何想法请帮助我。

以下代码可能会帮助您:

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

从您的上下文来看,我假设您的设备没有安装Facebook应用程序,您也不希望使用iOS 6系统身份validation,这会使默认登录行为使用Safari。 如果您要清除Safari cookie,这应该有效,但为了在您的场景中获得更流畅的体验,您应该使用FBSession openWithBehavior:completionHandler:方法并指定FBSessionLoginBehaviorForcingWebview的行为,以便它使用内联webview对话框进行身份validation。

请参阅Facebook iOS SDK中的SwitchUserSample作为示例,因为该示例演示了可以在多个帐户之间切换的应用程序。

https://developers.facebook.com/docs/ios/login/#logout

https://developers.facebook.com/docs/ios/ios-sdk-tutorial/