1

我正在创建一个简单的 iOS 应用程序,它登录到 facebook,获取自己和朋友的详细信息,从 facebook 注销。

现在从 Facebook 登录并获取自己和朋友的详细信息工作正常,但我无法从 Facebook 完全注销。每当我注销然后重新登录时 - 我看到的是授权屏幕而不是 Facebook(Web)的身份验证登录屏幕。

下面是我的代码 - 你能看看我从登录到获取自己和朋友的详细信息以及从 Facebook 注销的步骤中是否有问题

1) 使用以下方法登录 Facebook

- (void)openSession
{
    if(FBSession.activeSession.isOpen)
    {
        [FBSession openActiveSessionWithReadPermissions:nil
                                           allowLoginUI:NO
                                      completionHandler:
         ^(FBSession *session,
           FBSessionState state, NSError *error)
         {
             [self sessionStateChanged:session state:state error:error];
         }];
    }
    else
    {
        [FBSession openActiveSessionWithReadPermissions:nil
                                           allowLoginUI:YES         
                                      completionHandler:
         ^(FBSession *session,
           FBSessionState state, NSError *error)
         {
             [self sessionStateChanged:session state:state error:error];
         }];
    }
}

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
        {
            // Connection is Open
            lblStatus.text = @"FBSessionStateOpen";
        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
        {
            [FBSession.activeSession closeAndClearTokenInformation];

            // Connection is Closed / Login Failed
            lblStatus.text = @"FBSessionStateClosed";
        }
            break;
        default:
            break;
    }
}

2)使用以下方法获取自己的详细信息和朋友的详细信息

if (FBSession.activeSession.isOpen)
{
[[FBRequest requestForMe] startWithCompletionHandler:
 ^(FBRequestConnection *connection,
   NSDictionary<FBGraphUser> *user,
   NSError *error) {
     if (!error) {
         self.lblSelfDetails.text = user.name;
         self.profilePicture.profileID = user.id;
     }
 }];
}

FBRequest *friendRequest = [FBRequest requestForGraphPath:@"me/friends?fields=name,birthday"];

[friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
    NSArray *data = [result objectForKey:@"data"];

    for (FBGraphObject<FBGraphUser> *friend in data)
    {
        NSLog(@"%@:%@", [friend name],[friend birthday]);
    }
}];

3)使用以下方法注销

- (IBAction)logout:(id)sender
{
    [FBSession.activeSession closeAndClearTokenInformation];
}
4

2 回答 2

1

请使用此方法从facebook成功登出

- (void)logout:(id<FBSessionDelegate>)delegate {

      _sessionDelegate = delegate;

      NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
      [self requestWithMethodName:@"auth.expireSession"
                        andParams:params andHttpMethod:@"GET"
                      andDelegate:nil];

      [params release];
      [_accessToken release];
      _accessToken = nil;
      [_expirationDate release];
      _expirationDate = nil;

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

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

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

希望这对你有帮助!!!

于 2013-07-26T12:21:30.307 回答
0

在 appdelegate.m 中添加这个方法

- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication 
         annotation:(id)annotation 
{
    return [FBSession.activeSession handleOpenURL:url]; 
}

然后将这些方法添加到您的 .m 文件中

- (void)sessionStateChanged:(FBSession *)session 
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen: {
                UIViewController *topViewController = 
                    [self.navController topViewController];
                if ([[topViewController modalViewController] 
                    isKindOfClass:[SCLoginViewController class]]) {
                    [topViewController dismissModalViewControllerAnimated:YES];
                }
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            // Once the user has logged in, we want them to 
            // be looking at the root view.
            [self.navController popToRootViewControllerAnimated:NO];

            [FBSession.activeSession closeAndClearTokenInformation];

            [self showLoginView];
            break;
        default:
            break;
    }

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                     initWithTitle:@"Error"
                           message:error.localizedDescription
                          delegate:nil
                 cancelButtonTitle:@"OK"
                 otherButtonTitles:nil];
        [alertView show];
    }    
}

- (void)openSession
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
                                       ^(FBSession *session, 
                                         FBSessionState state, NSError *error) {
                  [self sessionStateChanged:session state:state error:error];
    }];    
}

接下来,创建一个新方法来关闭当前会话并注销一个人:

    -(void)logoutButtonWasPressed:(id)sender
    {
        [FBSession.activeSession closeAndClearTokenInformation];
    }
于 2013-07-26T12:24:55.703 回答