How to Log out from Facebook or revoke login using Parse and Swift

5.7k views Asked by At

I'm trying to completely log my user out of my app. When using

PFUser.logout()

I successfully log the user out of Parse. But when I come back in the app and click on the login button, I'm redirected to the Facebook screen saying :"You have already authorized [name of the app]" So I can never login again with another account, except if I reset the simulator.

I also tried to logout using

PFFacebookUtils.facebookLoginManager().logOut()

But it doesn't work either... Hope you can help me find a solution to this ! I'm using the latest Facebook and Parse SDK

Update:

Actually using PFUser.logout() and the last versions of Parse and the Facebook iOS SDK version >= 4.4.0 will do the job.

Do not use PFFacebookUtils.facebookLoginManager().logOut() or FBSDKLoginManager().logOut(), because it will only log the user out from Facebook but not from the Parse back-end. Which means that it will not delete the revocable session on the Parse back-end.

3

There are 3 answers

4
King-Wizard On BEST ANSWER

Actually there is a difference between Revoking Login (letting people completely de-authorizing an app, or revoking login) and Logging out an user from his/her Facebook account:

Revoking Login

(you can check the Facebook documentation)

You can also let people completely de-authorize an app, or revoke login, by making a call to this Graph API endpoint:

DELETE /{user-id}/permissions

This request must be made with a valid user access token or an app access token for the current app. If the request is successful, your app receives a response of true. If the call is successful, any user access token for the person will be invalidated and they will have to log in again. Because you're de-authorizing your app, they will also have to grant access to your app as if they were logging in for the first time.

let facebookRequest: FBSDKGraphRequest! = FBSDKGraphRequest(graphPath: "/me/permissions", parameters: nil, HTTPMethod: "DELETE")

facebookRequest.startWithCompletionHandler { (connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in

                if(error == nil && result != nil){
                    println("Permission successfully revoked. This app will no longer post to Facebook on your behalf.")
                    println("result = \(result)")
                } else {
                    if let error: NSError = error {
                        if let errorString = error.userInfo?["error"] as? String {
                            println("errorString variable equals: \(errorString)")
                        }
                    } else {
                        println("No value for error key")
                    }
                }
}

Logging out an user form his/her Facebook account

If you have used Parse and the Facebook iOS SDK version >= 4.4.0 to sign up or log in a user via Facebook, and you do not want to de-authorize an app, or revoke login, but just want to log out the user from Facebook, then please use instead:

PFUser.logOut()

It will log out the user, delete the session on the Parse back-end side (do not forget to enable Parse revocable session via your Parse app settings), plus it will also delete the Facebook session written on the user device's disk.

I hope this answer will help you guys.

5
Eddy Liu On

Source: Logout from facebook in iOS app

- (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];
  }
}
3
Ahsan Ebrahim Khatri On

I just implemented this. Call this function from any viewController in the application and it will logout from facebook. Worked like a charm for me. I hope it helps you.

-(void) logout
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

    if ( [FBSDKAccessToken currentAccessToken] ){
        [login logOut];
    }
}