Created
February 18, 2024 00:44
-
-
Save MarcoEidinger/2b1c0320e663ea32ba22b8d16555855f to your computer and use it in GitHub Desktop.
A decorator that ensures a biometric prompt in the simulator by explicitly calling LAContext.evaluatePolicy(_:localizedReason:). Helpful to improve test experience for protected keychain items in a simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func localAuthenticationInSimulator<T>( | |
evaluating policy: LAPolicy = .deviceOwnerAuthentication, | |
on context: LAContext = LAContext(), | |
beforeCalling function: @escaping () -> T? | |
) async -> T? { | |
#if targetEnvironment(simulator) | |
var localizedReason = context.localizedReason | |
if localizedReason.isEmpty { | |
localizedReason = "Test in simulator" | |
context.localizedReason = localizedReason | |
} | |
guard context.canEvaluatePolicy(policy, error: nil) else { return nil } | |
do { | |
guard try await context.evaluatePolicy(policy, localizedReason: localizedReason) else { return nil } | |
return function() | |
} catch { | |
return nil | |
} | |
#else | |
return function() | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment