简体   繁体   中英

Forceful login in Facebook iOS SDK

I have an iPad app and its used in a hotel for guests. So multiple users use the app through Facebook login and after the usage I need to logout the user from the app. So the next user will have the login screen again. I'm doing this in logout process and when I call the login function Its not giving the login screen. Instead it give me the already authorized screen. (With ok and cancel button). Please help to resolve this issue.

To login:

FBSDKLoginManager().logInWithReadPermissions(["email", "public_profile"], fromViewController: self, handler: { (result, error) -> Void in
if error != nil {
    print("error : \(error.localizedDescription)")
} else if result.isCancelled {
    print("user cancelled")
} else {
    print("success")
}
})

To Logout :

FBSDKLoginManager().logOut()
FBSDKAccessToken.setCurrentAccessToken(nil)
FBSDKProfile.setCurrentProfile(nil)

let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies! {
    storage.deleteCookie(cookie)
}
NSUserDefaults.standardUserDefaults().synchronize()

It will always give you an already authorized screen because Facebook is not responsible to logout from the Safari browser.

You are logged out from the login manager it is fine.

For this situation you can use the loginBehavior property of FBSDKLoginManager

You have to set the behavior to the Web it will open popup to login.

let fbManager : FBSDKLoginManager = FBSDKLoginManager()
fbManager.loginBehavior = FBSDKLoginBehavior.Web
fbManager.logInWithReadPermissions(["email"], fromViewController: self) { (result, error) -> Void in
    if error != nil {
        print(error)
    }
    else {
        print(result)
    }
}

在此处输入图片说明

You can do the logout from the manager as per your need.

Hope it will help you.

clear your facebook token from ios like this... this make user to login every time in app

ACAccountStore *store = [[ACAccountStore alloc] init];
        NSArray *fbAccounts = [store accountsWithAccountType:[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]];
        for (ACAccount *fb in fbAccounts) {
            [store renewCredentialsForAccount:fb completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {

            }];
        }

Here is a working solution using their custom button class FBSDKLoginButton on a view. You should only enter the protected page on view will appear if the currentAccessToken is not nil. This will prevent showing the first view for a few miliseconds. As you can see, I embeded my viewControllers in a NaviguationController.

UIView with custom class in the login view :

在此处输入图片说明

ViewWillAppear :

override func viewWillAppear(animated: Bool) {
    super.viewDidAppear(animated)
    if (FBSDKAccessToken.currentAccessToken() != nil) {
        loginFacebook(FBSDKAccessToken.currentAccessToken().tokenString,
            userId: FBSDKAccessToken.currentAccessToken().userID)
        self.enterProtectedPage();
    }
}

ViewDidLoad :

override func viewDidLoad() {
        super.viewDidLoad()
        loginBtn.delegate = self
        loginBtn.readPermissions = ["public_profile", "email", "user_birthday", "user_relationship_details"];
}

login button click action :

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
    if (error != nil) {
       print(error.localizedDescription)
       return
    }

    if let _ = result.token {
       loginFacebook(FBSDKAccessToken.currentAccessToken().tokenString,
       userId: FBSDKAccessToken.currentAccessToken().userID)
       self.enterProtectedPage();
    }
 }

enter protected page :

func enterProtectedPage() {
     let protectedPage = self.storyboard?.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as!
        ProtectedPageViewController
        let protectedPageNav = UINavigationController(rootViewController: protectedPage)
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = protectedPageNav
}

logout action on the protected page :

@IBAction func btnLogoutClicked(sender: AnyObject) {
        let loginManager = FBSDKLoginManager()
        loginManager.logOut()
        let loginPage = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as!ViewController
        let loginPageNav = UINavigationController(rootViewController: loginPage)
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.window?.rootViewController = loginPageNav
}

This works for me For objective-C

1st Define

@property(nonatomic,strong)FBSDKLoginManager *login;

Then Use this method.

-(void)logoutFromFacebook{

   if(self.login){

      [self.login logOut];
  }else{
      self.login = [[FBSDKLoginManager alloc] init];
      [self.login logOut];
  }

}

For Swift

var login: FBSDKLoginManager?

func FBSDKLoginManager () {
  if self.login != nil {
    self.login.logout()
  }else {
    self.login = self.login()
    self.login.logout()
  }
}

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM