且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS KeyChain 未从后台检索值

更新时间:2023-12-03 20:00:40

我的问题接近于原因,但不完全是.在阅读了一个又一个博客,一个又一个教程之后,我终于找到了一个暗示可能会发生什么的文章.

My question was close to the mark for the reason why, but not quite. After reading through blog after blog, tutorial after tutorial, I finally found one that gave off a hint of what might be happening.

锁定的主屏幕.钥匙串教程总是将钥匙串的可访问性设置留空,因此它默认为 Apple 的最低/最安全的访问级别.但是,如果用户在锁定屏幕上有密码,则此级别不允许访问钥匙串.答对了!这解释了偶发行为以及为什么这只发生在一小部分用户身上.

Locked home screens. The keychain tutorials always left the accessibility settings for the keychain blank, so it would default to Apple's lowest/safest access level. This level however doesn't allow keychain access if the user has a passcode on the lock screen. Bingo! This explains the sporadic behavior and why this only happens to a small percentage of users.

一行代码,解决所有问题.

One line of code, solves the entire mess.

[wrapper setObject:(__bridge id)kSecAttrAccessibleAlways forKey:(__bridge id)kSecAttrAccessible];

在我设置用户名和密码值的地方添加这一行.奇迹般有效.希望这会帮助那里的人.它让我困惑了很长一段时间,直到我能够将这些碎片拼凑在一起.

Add this line where I'm setting the username and password values. Works like a charm. Hope this will help someone out there. It confounded me for quite a while until I was able to put the pieces together.