title: Auto login in iOS Swift using Facebook & Google
body: <p>I know this is a design issue and not really a technical one, but it sounds like a very common issue, so I assume it is appropriate for here also.</p>
  
  <p>I have an iOS application (written in Swift) that should support 3 login methods: Facebook, Google &amp; signing up using email &amp; password.</p>
  
  <p>Once user logged in for the first time I want to remember which method he used and login automatically next time the app starts (assuming it didn't stay in background of course).</p>
  
  <p>For Google I can use the following code:</p>
  
  <pre><code>let googleSignIn = GPPSignIn.sharedInstance()
  if googleSignIn.trySilentAuthentication() {
      // was able to login automatically using google
      // no need to show login screen
  }
  </code></pre>
  
  <p>This code is UI-independent, meaning I can call it from AppDelegate and know in advance whether I should show login screen or skip automatically to application's main screen.</p>
  
  <p>It looks that the equivalent functionality in Facebook is to have ViewController which implements <code>FBLoginViewDelegate</code> protocol, has <code>FBLoginView</code> button, and assign itself as delegate to FBLoginView:</p>
  
  <pre><code>class LoginViewController: UIViewController, FBLoginViewDelegate, GPPSignInDelegate {
  
      @IBOutlet weak var fbLoginView: FBLoginView!
  
      override func viewDidLoad() {
          self.fbLoginView.delegate = self
  
       }
   ...
  </code></pre>
  
  <p>For Facebook auto login I can then use the following code in this view controller:</p>
  
  <pre><code>func loginViewFetchedUserInfo(loginView : FBLoginView!, user: FBGraphUser) {
        // logged in automatically using facebook, open main page
   }
  </code></pre>
  
  <p>However, this code is UI-<strong>dependent</strong>, meaning I must instantiate the ViewController, otherwise loginViewFetchedUserInfo callback won't trigger and I won't know that user logged in automatically using Facebook.</p>
  
  <p>Of course if I want to handle the auto-login after user signed up using email &amp; password I need to manage it myself, what I can do in this case is simply save the credentials using <code>NSUserDefaults</code>.</p>
  
  <p>Basically I can use the same mechanism also to remember which method the user used to login, but I guess that for Facebook &amp; Google I should still call their appropriate API to actually perform the auto-login.</p>
  
  <p>The issue I'm struggling with is that for Facebook there's no UI-<strong>independent</strong> API like in Google, and I must "pass through" the login screen, although I may not need it eventually.</p>
  
  <p>Another problem is that the Facebook event is triggered asynchronously, so I need to "suspend" the login buttons presentations until it's triggered, although I can know easily that user already logged in to Facebook in the past, by keeping indication using NSUserDefaults, as I do for sign-up case, and I want to do anyway, to know which API I should use to verify the automatic login.</p>
  
  <p>Regarding Google login, it's not clear if the value returned from trySilentAuthentication is enough to verify the login, or if I also need to wait for <code>GPPSignInDelegate.finishedWithAuth</code> to trigger, so need to instantiate the LoginView also in this case.</p>
  
  <p>This turned to be a very long question, but again, it sounds like a very common scenario, so hopefully I can get some insights.</p>