facebook ios sdk 无法登出bug的解决方法

程沛
2023-12-01

先说下facebook ios sdk中如何不跳到safari而在程序内登陆

//  UIDevice *device = [UIDevice currentDevice];
//  if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
//    if (tryFBAppAuth) {
//      NSString *scheme = kFBAppAuthURLScheme;
//      if (_urlSchemeSuffix) {
//        scheme = [scheme stringByAppendingString:@"2"];
//      }
//      NSString *urlPrefix = [NSString stringWithFormat:@"%@://%@", scheme, kFBAppAuthURLPath];
//      NSString *fbAppUrl = [FBRequest serializeURL:urlPrefix params:params];
//      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
//    }
//
//    if (trySafariAuth && !didOpenOtherApp) {
//      NSString *nextUrl = [self getOwnBaseUrl];
//      [params setValue:nextUrl forKey:@"redirect_uri"];
//
//      NSString *fbAppUrl = [FBRequest serializeURL:loginDialogURL params:params];
//      didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
//    }
//  }

在Facbook.m文件中,注释掉上面那段即可,我的版本是241到260行


然后说下无法登出的原因,先看下登出时调用的方法:

- (void)logout {
    [self invalidateSession];
        
    if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogout)]) {
        [self.sessionDelegate fbDidLogout];
    }
}

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

这时候我们发现他只是销毁掉了http://login.facebook.com下的cookie,所以我们的账号有时会无法正常登出,因为他没销毁https://login.facebook.com下的cookie!,所以我们需要在invalidateSession中加入如下代码:

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

简单起见我就加了个1,大家起名不要这样啊!


 类似资料: