title: How to handle touchID when loading app from background Swift
body: <p>I'm implementing the login possibility with touchID using Swift.
  Following: when the App is started, there is a login screen and a touchID popup - that's working fine. The problem occurs, when the app is loaded from background: I want the touchID popup appear <strong>over a login screen</strong> if a specific timespan hasn't been exceeded yet - but this time I want the touchID to go to the last shown view before the app entered background. (i.e. if the user wants to cancel the touchID, there is a login screen underneath where he then can authenticate via password, which leads him to the last shown view <strong>OR</strong> if the touchID authentication succeeded, the login screen should be dismissed and the last shown view presented.)
  I really tried everything on my own, and searched for answers - nothing did help me. Here is my code:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      //notify when foreground or background have been entered -&gt; in that case there are two methods that will be invoked: willEnterForeground and didEnterBackground
      let notificationCenter = NSNotificationCenter.defaultCenter()
      notificationCenter.addObserver(self, selector: "willEnterForeground", name:UIApplicationWillEnterForegroundNotification, object: nil)
      notificationCenter.addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
      password.secureTextEntry = true
      if (username != nil) {
          username.text = "bucketFit"
      }
      username.delegate = self
      password.delegate = self
  
      if let alreadyShown : AnyObject? = def.objectForKey("alreadyShown") {
          if (alreadyShown == nil){
              authenticateWithTouchID()
          }
      }
  }
  </code></pre>
  
  <p><strong>willEnterForeground:</strong></p>
  
  <pre><code>func willEnterForeground() {
      //save locally that the guide already logged in once and the application is just entering foreground
      //the variable alreadyShown is used for presenting the touchID, see viewDidAppear method
      def.setObject(true, forKey: "alreadyShown")
      if let backgroundEntered : AnyObject? = def.objectForKey("backgroundEntered") {
          let startTime = backgroundEntered as! NSDate
          //number of seconds the app was in the background
          let inactivityDuration = NSDate().timeIntervalSinceDate(startTime)
          //if the app was longer than 3 minutes inactiv, ask the guide to input his password
          if (inactivityDuration &gt; 2) {
              showLoginView()
          } else {
              def.removeObjectForKey("alreadyShown")
              showLoginView()
          }
      }
  }
  </code></pre>
  
  <p><strong>authenticateWithTouchID():</strong></p>
  
  <pre><code>func authenticateWithTouchID() {
      let context : LAContext = LAContext()
      context.localizedFallbackTitle = ""
      var error : NSError?
      let myLocalizedReasonString : NSString = "Authentication is required"
      //check whether the iphone has the touchID possibility at all
      if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &amp;error) {
          //if yes then execute the touchID and see whether the finger print matches
          context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString as String, reply: { (success : Bool, evaluationError : NSError?) -&gt; Void in
              //touchID succeded -&gt; go to students list page
              if success {
                  NSOperationQueue.mainQueue().addOperationWithBlock({ () -&gt; Void in
                      self.performSegueWithIdentifier("studentsList", sender: self)
                  })
              } else {
                  // Authentification failed
                  print(evaluationError?.description)
                  //print out the specific error
                  switch evaluationError!.code {
                  case LAError.SystemCancel.rawValue:
                      print("Authentication cancelled by the system")
                  case LAError.UserCancel.rawValue:
                      print("Authentication cancelled by the user")
                  default:
                      print("Authentication failed")
                  }
              }
          })
      }
  }
  </code></pre>
  
  <p><strong>shouldPerformSegueWithIdentifier:</strong></p>
  
  <pre><code>override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -&gt; Bool {
      if (false) { //TODO -&gt; username.text!.isEmpty || password.text!.isEmpty
          notify("Login failed", message: "Please enter your username and password to proceed")
          return false
      } else if (false) { //TODO when backend ready! -&gt; !login("bucketFit", password: "test")
          notify("Incorrect username or password", message: "Please try again")
          return false
          //if the login page is loaded after background, dont proceed (then we need to present the last presented view on the stack before the app leaved to background)
      } else if let alreadyShown : AnyObject? = def.objectForKey("alreadyShown") {
          if (alreadyShown != nil){
              //TODO check whether login data is correct
              dismissLoginView()
              return false
          }
      }
  
      return true
  }
  </code></pre>
  
  <p><strong>Thank you in advance.</strong></p>