Bartłomiej Janczak
Bartłomiej Janczak

Reputation: 21

Xcode passcode - swift

How do I get a system access code? I want to block the controller in the application. I do not want the user to create a new code but I want to use the system code.

I saw that other applications have such feature

Example

Upvotes: -2

Views: 520

Answers (1)

Will
Will

Reputation: 5460

You can't get the users passcode, however you can authenticate a user and have them use their TouchID/FaceID

For this, you'll want to use the LocalAuthentication framework.

Here's an example:

let myContext = LAContext()
let myLocalizedReasonString = <#String explaining why app needs authentication#>

var authError: NSError?
if #available(iOS 8.0, macOS 10.12.1, *) {
    if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
        myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
            if success {
                // User authenticated successfully, take appropriate action
            } else {
                // User did not authenticate successfully, look at error and take appropriate action
            }
        }
    } else {
        // Could not evaluate policy; look at authError and present an appropriate message to user
    }
} else {
    // Fallback on earlier versions
}

Credit: Apple

Upvotes: 2

Related Questions