0

I am currently able to login to my app using the FBSDKLogin framework successfully. I've also assigned my ViewController that the user logs in from to be the delegate, and then capture the necessary parameters that I need from the user. My problem now is that after the user logs in, they are taken to another screen, and should they want to logout of the app, they must do so from this other screen.

In order to log the user out of Facebook from this other screen, what I've tried to do is first capture the reference to the Facebook login button (which they use to login with initially) and store it in my NSUserDefaults, and then later retrieve this reference from the latter screen in order to log the user out. This is how I'm trying to capture the reference:

extension FirstViewController: FBSDKLoginButtonDelegate {

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("Did log out of facebook")
    }

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if error != nil {
            print(error)
            return
        }

        let defaults = UserDefaults.standard
        defaults.set(loginButton, forKey: "fbLoginButton")
        ...


    }
}

What I do later is make this second View Controller (from which the user is allowed to log out of the app from), also as a delegate for the FBSDKLoginButton, and try to call the logout delegate method like so:

fileprivate func logoutUser() {

        //logout user if logged in via Facebook
        guard let fbLoginButton = UserDefaults.standard.object(forKey: "fbLoginButton") as? FBSDKLoginButton! else { return }
        loginButtonDidLogOut(fbLoginButton)
    }

extension SecondViewController: FBSDKLoginButtonDelegate {

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("Did log out of facebook")
    }

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    }
}

Unfortunately, when I run my app, and try to login using the FBSDK, I get the following runtime error:

[User Defaults] Attempt to set a non-property-list object <FBSDKLoginButton: 0x7ffc64363720; baseClass = UIButton; frame = (10 565; 355 50); opaque = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = <CALayer: 0x608000436f40>> as an NSUserDefaults/CFPreferences value for key fbLoginButton
2017-07-16 14:57:12.623 MyApp[4066:334278] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object <FBSDKLoginButton: 0x7ffc64363720; baseClass = UIButton; frame = (10 565; 355 50); opaque = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = <CALayer: 0x608000436f40>> for key fbLoginButton'

Can anyone see what it is I'm doing wrong? Is my approach to log the user out of Facebook correct, or should I be doing it some other way?

halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74

0 Answers0