title: Swift - AWS Cognito-Facebook Login
body: <p>I want to use Facebook for my login process with Cognito, and I've followed a lot of AWS documentation and look at tutorials and questions in Stackoverflow, but I've not found a solution for my problem.</p>
  
  <p>When the user opens the app, it will check if the user is logged in using IdentityManager. If not, it will open a new view where the user can sign in using Facebook using Facebook SDK. After that, I stored the token with a custom IdentityProvider as the documentation said (credentialsProvider.logins is deprecated). Everything seems to work fine, but every time I reopen the application, my session isn't restored. </p>
  
  <p>I found out that if I use <code>AWSIdentityManager.defaultIdentityManager().resumeSessionWithCompletionHandler(handler)</code>I restored my session, but in case the user isn't logged in, it doesn't show my custom login screen as expected, but a Safari web view to Facebook.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>AppDelegate.swift</strong></p>
  
  <pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
  
      let identityManager = AWSIdentityManager.defaultIdentityManager()
  
      identityManager.resumeSessionWithCompletionHandler({
          (result, error) -&gt; Void in
  
          if !identityManager.loggedIn {
              let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
              let initialViewControlleripad : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SignIn") as UIViewController
              self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
              self.window?.rootViewController = initialViewControlleripad
              self.window?.makeKeyAndVisible()
          }
      })
  
      return AWSMobileClient.sharedInstance.didFinishLaunching(application, withOptions: launchOptions)
  }
  </code></pre>
  
  <p><strong>SignInViewController.swift</strong></p>
  
  <pre><code>@IBAction func openFacebookLoginScreen(sender: AnyObject) {
      FBSDKLoginManager().logInWithReadPermissions(FACEBOOK_PERMISSIONS, fromViewController: self, handler: { (result, error) -&gt; Void in
  
          if error == nil {
              let fbLoginResult : FBSDKLoginManagerLoginResult = result
  
              if fbLoginResult.isCancelled {
                  print("Cancelled")
              }
              else {
                  if FBSDKAccessToken.currentAccessToken() != nil {
                      self.signInFacebook(FBSDKAccessToken.currentAccessToken().tokenString)
                      self.dismissSignInView()
                  }
              }
          }
      })
  }
  
  func signInFacebook(fbToken: String){
  
      let logins = [AWSIdentityProviderFacebook : fbToken]
  
      let customProviderManager = CustomIdentityProvider(tokens: logins)
  
      let credentialsProvider = AWSCognitoCredentialsProvider(
          regionType:.USEast1,
          identityPoolId: COGNITO_IDENTITY_POOL_ID,
          identityProviderManager: customProviderManager)
  
      let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider: credentialsProvider)
      AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
  }
  
  class CustomIdentityProvider: NSObject, AWSIdentityProviderManager {
      var tokens : [NSString : NSString]?
  
      init(tokens: [NSString : NSString]) {
          self.tokens = tokens
      }
  
      @objc func logins() -&gt; AWSTask {
         return AWSTask(result: tokens)
      }
  }
  </code></pre>