Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoEidinger/2b1c0320e663ea32ba22b8d16555855f to your computer and use it in GitHub Desktop.
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
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