Friday, 9 August 2019

Customize keychain alert actions

I'm wondering if customization of the keychain alert is possible? Below you can find the image:

enter image description here

What I'm trying to achieve:

When the authentication with FaceID/TouchID fails and pops above shown alert, I want user to tap Enter Passcode and show my custom Application Passcode UI, instead of device passcode (system ui). Basically, I want to customize the fallback action.

Context: I know it is possible with using LAContext, however due to the reasons that we can bypass it, I don't feel comfortable in implementing that, so I chose Keychain accessControlFlags.

Snapshot code, what I currently have:

func data(forAccount account: String, service: String, accessGroup: String?) throws -> Data? {
    guard let accessControl = createAccessControl(with: .userPresence) else { return nil }
    var query = self.query(forAccount: account, service: service, accessGroup: accessGroup)

    var authContext = LAContext()
    authContext.localizedFallbackTitle = "Enter Passcode"
    authContext.localizedCancelTitle = "Cancel"

    query[KeychainConstants.matchLimit] = KeychainConstants.matchLimitOne
    query[KeychainConstants.returnData] = kCFBooleanTrue
    query[KeychainConstants.authenticationContext] = authContext
    query[KeychainConstants.accessControl] = accessControl

    var result: AnyObject?
    let status = withUnsafeMutablePointer(to: &result) {
        securityItemManager.copyMatching(query, result: UnsafeMutablePointer($0))
    }

    if let error = error(fromStatus: status), error != .itemNotFound  {
        throw error
    }
    guard result != nil else { return nil }
    guard let resultData = result as? Data else { throw AccessError.invalidQueryResult }
    return resultData
}



from Customize keychain alert actions

No comments:

Post a Comment