Reputation: 157
I am trying to implement app lock on my iOS app written with swift 5 and using storyboard to create UI. I need to implement feature such that when user enables screen lock in settings of my app , the app lock activates and without biometrics success user cannot view app content , else app should work normally.
I have already implemented the authentication code using the following tutorial.
And I have implemented the same in SceneDelegate as follows :
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
debugPrint("Did become active")
self.biometricIDAuth.canEvaluate { (canEvaluate, _, canEvaluateError) in
guard canEvaluate else {
// Face ID/Touch ID may not be available or configured
return
}
self.biometricIDAuth.evaluate { [weak self] (success, error) in
guard success else {
// Face ID/Touch ID may not be configured
return
}
// You are successfully verified
debugPrint("Success biometric : \(success)")
}
}
}
where biometricIDAuth is class name. Currently the code only shows the authentication UI when entering foreground after several minutes in background . how to implement it such that every time the user comes into foreground the UI for the same shows and if possible how to blur the content when default authentication ui shows.
Upvotes: 0
Views: 959
Reputation: 159
This is used for faceid or touch id
import LocalAuthentication
func imageTapped() {
// Your action
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error){
let reason = "Identify Yourself!."
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { [weak self] (success,authError) in
DispatchQueue.main.async {
if success {
// This is success faceId succeed to identify user.
// do your work here
} else {
// biometric is avaliable but could not be verified.
// try to use custom password. or whatever you like.
}
}
}
} else {
// there is neither touch id or face id.
}
}
when user is going to background use notification center to pop to authenticationVC.
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(
self, selector: #selector(popToAuthVC),
name: UIApplication.willResignActiveNotification, object: nil
)
make sure your info.plist has this
Upvotes: 1