 import SnapKit
 import SwiftKeychainWrapper
 
+enum AuthenticationState {
+    case NotAuthenticating
+    case Presenting
+}
+
 class SensitiveViewController: UIViewController {
     var promptingForTouchID: Bool = false
     var backgroundedBlur: UIImageView?
+    var authState: AuthenticationState = .NotAuthenticating
 
     override func viewWillAppear(animated: Bool) {
         super.viewWillAppear(animated)
         let notificationCenter = NSNotificationCenter.defaultCenter()
         notificationCenter.addObserver(self, selector: #selector(SensitiveViewController.checkIfUserRequiresValidation), name: UIApplicationWillEnterForegroundNotification, object: nil)
+        notificationCenter.addObserver(self, selector: #selector(SensitiveViewController.checkIfUserRequiresValidation), name: UIApplicationDidBecomeActiveNotification, object: nil)
         notificationCenter.addObserver(self, selector: #selector(SensitiveViewController.blurContents), name: UIApplicationWillResignActiveNotification, object: nil)
     }
 

                AppAuthenticator.presentPasscodeAuthentication(self.navigationController, delegate: self)
            }
        )
    }

    func blurContents() {

        let notificationCenter = NSNotificationCenter.defaultCenter()
        notificationCenter.removeObserver(self, name: UIApplicationWillEnterForegroundNotification, object: nil)
        notificationCenter.removeObserver(self, name: UIApplicationWillResignActiveNotification, object: nil)
        notificationCenter.removeObserver(self, name: UIApplicationDidBecomeActiveNotification, object: nil)
    }

    func checkIfUserRequiresValidation() {
        guard authState != .Presenting else {
            return
        }

        presentedViewController?.dismissViewControllerAnimated(false, completion: nil)
        guard let authInfo = KeychainWrapper.authenticationInfo() where authInfo.requiresValidation() else {
            removeBackgroundedBlur()
