iOS KeyChain 未从后台检索值 [英] iOS KeyChain not retrieving values from background

查看:21
本文介绍了iOS KeyChain 未从后台检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前将用户名(电子邮件)以及电子邮件和密码的加盐哈希值存储在 iOS KeyChain 中.我正在使用 here 找到的 ARC'ified 版本.

I am currently storing the username (email) and a salted hash of the email and password in the iOS KeyChain. I'm using the ARC'ified version found here.

KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyCustomIdentifier" accessGroup:nil];
[wrapper setObject:APP_NAME forKey:(__bridge id)kSecAttrService];
[wrapper setObject:email forKey:(__bridge id)kSecAttrAccount];
[wrapper setObject:token forKey:(__bridge id)kSecValueData];

当我需要在应用程序处于活动状态时为我的网络调用提取令牌时,这一切都很好.它适用于从干净的启动登录,以及整个网络调用.当应用程序在后台时,问题就开始了.

This all works fine when I need to pull the token out for my network calls while the app is active. It works for logging in from a clean startup, as well as all the network calls throughout. The trouble starts when the app is in the background.

请记住,这只是偶尔发生,我还没有将其归结为特定的 iOS 版本或设备.

Keep in mind, this only happens sporadically and I have yet to pin it down to a specific iOS version or device.

用户旅行了一个位置(区域监控),我想用他们的状态更新服务器.我尝试将令牌从钥匙串中拉出,就像我对其他所有网络调用所做的那样,并更新状态.但对于一些用户来说,价值为零.没有它,我无法更新网络内容.为什么这对大多数人有效,但对一小部分人无效?

The user trips a location (region monitoring) and I want to update the server with their status. I try to pull the token out of the keychain, the same way I do for every other network call, and update the status. But for some users, the value is nil. Without it, I can't update the network stuff. Why would this work for most, but not for a small percentage?

KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyCustomIdentifier" accessGroup:nil];
NSString *token = [wrapper objectForKey:(__bridge id)kSecValueData];

我已经回到钥匙链包装器的非 ARC 版本,但我仍然得到相同的结果.我将不胜感激对此的任何反馈.这只是我用户的一小部分,但这是我想解决而不是担心的问题.提前致谢.

I've gone back to the non-ARC version of the keychainwrapper, but I still get the same results. I would appreciate any feedback on this. It is only a small part of my users, but it is an issue I would like to fix and not worry about. Thanks in advance.

另外,我所有的后台工作都设置在 backgroundTask 中,以防止事情超时.我对钥匙串周围的工作没有任何问题,但在我的令牌被填满之前我不会让事情继续进行.

Also, all of my background work is set up in a backgroundTask to prevent things from timing out. I'm not having any issues with the work surrounding the keychain, but I don't let things go forward until my token is filled.

编辑我已经解决了他们的钥匙串没有从后台检索值的问题.我将在下面发布答案并接受它,因为我觉得这个问题以后可能对其他人有价值.

EDIT I've figured out my issue with they keychain not retrieving values from the background. I will post the answer below and accept it as I feel this question may become valuable to others later.

推荐答案

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

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.

这篇关于iOS KeyChain 未从后台检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆