Facebook ios注销方法不起作用

时间:2012-04-28 22:07:52

标签: ios facebook

我在app中使用此方法登出facebook

- (void)fbDidLogout {
    // Remove saved authorization information if it exists
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]) {
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    }
    NSLog(@"logout success!");
}

此方法已被调用,但当我重新启动应用程序时,facebook仍然知道我的最新授权。

我的全面实施

我为facebook实例创建单例对象。

这是我的经理.h

#import <Foundation/Foundation.h>
#import "FBConnect.h"

@interface FacebookManager : NSObject <FBSessionDelegate> {
    Facebook *facebook;
}
@property (nonatomic, strong) Facebook *facebook;

+ (FacebookManager *)sharedInstance;
- (void)initWithAppID:(NSString *)appID;

@end

这是singleton facebookmanager .m

#import "FacebookManager.h"

@implementation FacebookManager
@synthesize facebook;

static FacebookManager *_sharedInstance = nil;

+ (FacebookManager *)sharedInstance {
    @synchronized(self) {
        if (!_sharedInstance) {
            _sharedInstance = [[FacebookManager alloc] init];
        }
    }
    return _sharedInstance;
}

- (void)initWithAppID:(NSString *)appID {
    facebook = [[Facebook alloc] initWithAppId:appID andDelegate:self];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }
    if (![facebook isSessionValid]) {
        [facebook authorize:nil];
    }
}

- (void)fbDidLogout {
    // Remove saved authorization information if it exists
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]) {
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    }
    NSLog(@"logout success!");
}

@end
在appDelegate中

我接下来:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    fbManager = [FacebookManager sharedInstance];
    [fbManager initWithAppID:@"myappid"];

... (some other code)

}

还将此代码添加到app delegate:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [fbManager.facebook handleOpenURL:url]; 
}

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[fbManager.facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[fbManager.facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];    
}

在其他视图控制器中,我将此方法称为注销:

- (IBAction)logoutFacebook:(id)sender {
    FacebookManager *fbManager = [FacebookManager sharedInstance];
    [fbManager.facebook logout];
}

也在plist中我添加了所需的url方案。

5 个答案:

答案 0 :(得分:5)

Facebook.m 代码修改为此代码,这对我有用。

-(void)logout:(id)delegate {

    self.sessionDelegate = delegate;
    [_accessToken release];
    _accessToken = nil;
    [_expirationDate release];
    _expirationDate = nil;

    NSHTTPCookieStorage *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:@"http://login.facebook.com"]];
    for (NSHTTPCookie* cookie in facebookCookies){
        [cookies deleteCookie:cookie];
    }

    //Adds this one.
    for (NSHTTPCookie *_cookie in cookies.cookies){
        NSRange domainRange  = [[_cookie domain] rangeOfString:@"facebook"];
        if(domainRange.length > 0){
            [cookies deleteCookie:_cookie];
        }
    }

    if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogout)]){
        [_sessionDelegate fbDidLogout];
    }
}

答案 1 :(得分:2)

你是什么意思&#34;当我重新启动应用程序时,facebook仍然知道我的最新授权&#34; ?这是否意味着您的FB accessstoken仍然有效?您仍然可以看到userdefaults值?到底是什么?

如果您的意思是当您再次触发FB登录时,它会自动重新登录,然后是如果您在iOS中安装了Facebook APP,则用户必须手动从FB应用程序注销才能切换一个帐户。

如果未安装FB应用程序,那么SSO应该在启动注销后提示用户再次登录。

答案 2 :(得分:1)

这似乎是sos / oauth 2.0的ios sdk api的工作方式。即使清除令牌后我也无法完全注销,但我无法从应用程序切换用户。必须转到FB应用程序切换用户

作为解决方法,更改Facebook.m中的行并禁用safariAuth

// [self authorizeWithFBAppAuth:YES safariAuth:YES];

 [self authorizeWithFBAppAuth:NO safariAuth:NO]

但是,每次授权时都必须输入用户名和密码。

答案 3 :(得分:1)

在Facebook.m中,添加以下代码以删除m.facebook.com域中的cookie。

- (void)invalidateSession {

   ...
    NSArray* facebookMCookies = [cookies cookiesForURL:
                                [NSURL URLWithString:@"https://m.facebook.com"]];

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

  ...
}

答案 4 :(得分:1)

我已经尝试了这段代码:

NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [paths objectAtIndex:0];
NSLog(@"cache dir %@", cachesDirectory);
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:cachesDirectory error:&error]) {
    BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@", cachesDirectory, file] error:&error];
    if (!success || error) {
        NSLog(@"Error delete file: %@, %@", file, error);
    } else {
        NSLog(@"Deleted file: %@", file);
    }
}