I am implementing Facebook integration using FBConnct and it works fine but when I want to log out from the Facebook it doesn't work.
My code is as follows:
- (IBAction)logOutbuttonPressed:(id)sender {
[Facebook logout:self];
}
- (void)fbDidLogout {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
NSLog(@" after %@",facebook.accessToken);
NSLog(@" date%@",facebook.expirationDate);
[defaults synchronize];
}
- (void)logout:(id<FBSessionDelegate>)delegate {
[self logout];
if (delegate != self.sessionDelegate &&
[delegate respondsToSelector:@selector(fbDidLogout)]) {
[delegate fbDidLogout];
}
+(void)fbDidLogout
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"])
{
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
// Hide the publish button.
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];
}
}
}
You need to implement this code for facebook logout as well
- (void)fbDidLogout {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"FBAccessTokenKey"];
[defaults removeObjectForKey:@"FBExpirationDateKey"];
NSLog(@" after %@",facebook.accessToken);
NSLog(@" date%@",facebook.expirationDate);
[defaults synchronize];
// Finding the Facebook Cookies and deleting them
NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:
[NSURL URLWithString:@"http://login.facebook.com"]];
for (NSHTTPCookie* cookie in facebookCookies) {
[cookies deleteCookie:cookie];
}
fbGraph = nil;
}
Try this import 'FBConnect.h' in ur second view controller
then .......
FBSession *session = [FBSession session]; [session logout];
Call this method to logout from facebook.
- (void)logOutFB {
fbGraph.accessToken = nil;
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];
}
}
}
来源:https://stackoverflow.com/questions/11167593/iphone-facebook-logout-feature-does-not-working