Facebook Connect iPhone API logout not working
I am attempting to write a Facebook integration in an iPhone app I'm working on. I have it logging in just fine, but I don't like开发者_开发百科 the idea of being able to turn a feature on without being able to turn it off. So, in working on the logout functionality, I have been caught in a snag.
- (IBAction) logoutClicked:(id)sender {
if (fbLoggedIn)
{
FBSession * mySession = [FBSession session];
[mySession logout];
}
}
- (void)sessionDidLogout:(FBSession*)session
{
NSLog(@"Session logged out.");
[theLoginButton setTitle:@"Facebook Time!" forState:UIControlStateNormal];
fbLoggedIn = FALSE;
theLogoutButton.enabled = NO;
theLogoutButton.alpha = 0;
}
The logoutClicked method responds to a button in my xib. The delegate method is not getting called. I have tried setting the Facebook session as a property in my ViewController in order to store/access the data across methods, but that didn't seem to work either. Anybody have any solutions?
Is the sessionDidLogout implemented in a class which implements FBSessionDelegate? And is it an instance of that class that you passed as a delegate when creating the session with the method [FBSession sessionForApplication:@"XXX" secret:@"YYY" delegate:(DELEGATE)] ?
This works for me:
(void)logout {
//self.sessionDelegate = delegate;
appDelegate.facebook.accessToken = nil;
appDelegate.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];
}
NSLog(@"Log out");
// Remove saved authorization information if it exists
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults objectForKey:@"FBAccessTokenKey"]) {
[userDefaults removeObjectForKey:@"FBAccessTokenKey"];
[userDefaults removeObjectForKey:@"FBExpirationDateKey"];
[userDefaults synchronize];
}
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];
}
}
}
Put this code for logout. I got this from this link.
- (void) fbDidLogout {
NSLog(@"Log out");
// Remove saved authorization information if it exists
if ([userDefaults objectForKey:@"FBAccessTokenKey"]) {
[userDefaults removeObjectForKey:@"FBAccessTokenKey"];
[userDefaults removeObjectForKey:@"FBExpirationDateKey"];
[userDefaults synchronize];
}
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];
}
}
}
精彩评论