title: Trying to logout of app using Facebook Login iOS SDK in Swift
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>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")
          ...
  
  
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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!) {
      }
  }
  </code></pre>
  
  <p>Unfortunately, when I run my app, and try to login using the FBSDK, I get the following runtime error:</p>
  
  <pre><code>[User Defaults] Attempt to set a non-property-list object &lt;FBSDKLoginButton: 0x7ffc64363720; baseClass = UIButton; frame = (10 565; 355 50); opaque = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = &lt;CALayer: 0x608000436f40&gt;&gt; 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 &lt;FBSDKLoginButton: 0x7ffc64363720; baseClass = UIButton; frame = (10 565; 355 50); opaque = NO; tintColor = UIExtendedGrayColorSpace 1 1; layer = &lt;CALayer: 0x608000436f40&gt;&gt; for key fbLoginButton'
  </code></pre>
  
  <p>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?</p>