Logout from facebook in iOS app

纵然是瞬间 提交于 2019-12-11 05:42:38

问题


I am creating a simple iOS App that Login to facebook, Fetch self and friends details, Logout from facebook.

Now Login from Facebook and Fetching self & friends details is working ok, but I am not able to logout fully from facebook. Whenever I logout and then Login back - I see the Authorization screen instead of Authentication Login screen of Facebook (of Web).

Below is my code - can you please see if there is something wrong in the steps I have performed from Login, to Fetching self & friends details and Logging out from facebook

1) Login to Facebook using below method

- (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) Fetching self details and Friends details using below method

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) Logout using below method

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

回答1:


Please use this method to logut successfully from the 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];
      }
    }

Hope this helps you!!!




回答2:


Add this mathod in appdelegate.m

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

Then Add these methods in your .m file

- (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];
    }];    
}

Next, create a new method that will close the current session and log a person out:

    -(void)logoutButtonWasPressed:(id)sender
    {
        [FBSession.activeSession closeAndClearTokenInformation];
    }


来源:https://stackoverflow.com/questions/17880993/logout-from-facebook-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!