我有一个使用钥匙串的现有 iOS 应用程序。在后台访问钥匙串时“抛出-25308错误”。我通过将“kSecAttrAccessible”属性添加为“kSecAttrAccessibleAfterFirstUnlock”来解决这个问题。 但是设置此属性后,应用程序在 iOS 17.3 版本中启动抛出 -25300 错误(到目前为止仅在 iOS 17.3 中观察到这种情况,在 iOS 17.2 中运行良好) 以下是应用程序中使用的钥匙串查询
var defaults: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: service,
kSecAttrAccount: account,
kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlock,
]
此外,该应用程序没有添加其他后台功能。有人可以帮助我理解为什么在设置“kSecAttrAccessibleAfterFirstUnlock”时出现错误-25300吗。 我将不胜感激任何帮助。谢谢。
kSecAttrAccessibleAfterFirstUnlock
作为辅助功能选项通常是正确的方法,以确保您的应用程序可以在后台运行时访问钥匙串项目,只要设备在重新启动后已解锁一次。
-25300
表示找不到该项目,需要更多详细信息来确定根本原因。
您可能需要考虑在尝试查询或更新该项目之前先检查该项目是否存在。检查钥匙串项目是否存在
func keychainItemExists(service: String, account: String) -> Bool {
let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrService: service,
kSecAttrAccount: account,
kSecReturnAttributes: kCFBooleanTrue!,
kSecMatchLimit: kSecMatchLimitOne
]
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
return status == errSecSuccess
}