ios - SecItemAdd 和 SecItemCopyMatching 返回错误代码 -34018 (errSecMissingEntitlement)

标签 ios cocoa keychain sskeychain

有时,当我从 Xcode 在设备上运行应用程序时,我会尝试访问钥匙串(keychain),但由于错误 -34018 而失败。这与任何记录在案的钥匙串(keychain)错误代码都不匹配,并且无法始终如一地重现。 (可能有 30% 的时间发生,我不清楚为什么会发生)。使调试这个问题非常困难的是完全缺乏文档。知道是什么原因造成的以及如何解决吗?我正在使用 Xcode 5 并在设备上运行 iOS 7.0.4。

这里有一个 Unresolved 问题:https://github.com/soffes/sskeychain/issues/52

编辑:为每个请求添加钥匙串(keychain)访问代码

我正在使用 SSKeychain 库来连接钥匙串(keychain)。这是片段。

#define SERVICE @"default"

@implementation SSKeychain (EXT)

+ (void)setValue:(NSString *)value forKey:(NSString *)key {
    NSError *error = nil;
    BOOL success = NO;
    if (value) {
        success = [self setPassword:value forService:SERVICE account:key error:&error];
    } else {
        success = [self deletePasswordForService:SERVICE account:key error:&error];
    }
    NSAssert(success, @"Unable to set keychain value %@ for key %@ error %@", value, key, error);
    if (!success) {
        LogError(@"Unable to set value to keychain %@", error);
    }
    LogTrace(@"Will set keychain account %@. is to nil? %d", key, value == nil);
    if (value == nil)
        LogWarn(@"Setting keychain %@ to nil!!!", key);
}

+ (NSString *)valueForKey:(NSString *)key {
    NSError *error = nil;
    NSString *value = [self passwordForService:SERVICE account:key error:&error];
    if (error && error.code != errSecItemNotFound) {
        NSAssert(!error, @"Unable to retrieve keychain value for key %@ error %@", key, error);
        LogError(@"Unable to retrieve keychain value for key %@ error %@", key, error);
    }
    return value;
}

+ (BOOL)removeAllValues {
    LogInfo(@"Completely Reseting Keychain");
    return [[self accountsForService:SERVICE] all:^BOOL(NSDictionary *accountInfo) {
        return [self deletePasswordForService:SERVICE account:accountInfo[@"acct"]];
    }];
}

@end

绝大多数情况下都很好。有时我会遇到无法写入或读取钥匙串(keychain)的断言失败,从而导致严重的断言失败。

最佳答案

iOS 10 / XCode 8 Fix:

Add KeyChain Entitlement, Go to project settings->Capabilities->Keychain Sharing->Add Keychain Groups+Turn On

Apple 的回答:

UPDATE: We have finally been able to reproduce the -34018 error on iOS 8.3. This is the first step in identifying the root cause and then coming up with a fix.

As usual, we can't commit to a release timeframe, but this has affected many developers and we really want to get this resolved.

Earlier I suggested adding a small delay in application:didFinishLaunchingWithOptions and applicationDidBecomeActive: before accessing the keychain as a workaround. However, that doesn't actually appear to help. That means that there's no known workaround at this time other than relaunching the app.

The issue appears to be related to memory pressure, so perhaps being more aggressive in handling memory warnings may alleviate the problem

https://forums.developer.apple.com/thread/4743#14441

更新

OK, here’s the latest.
This is a complex problem with multiple possible causes:

  • Some instances of the problem are caused by incorrect app signing. You can easily distinguish this case because the problem is 100% reproducible.
  • Some instances of the problem are caused by a bug in how iOS supports app development (r. 23,991,853). Debugging this was complicated by the fact that another bug in the OS (r. 23,770,418) masked its effect, meaning the problem only cropped up when the device was under memory pressure. We believe these problems were resolved in iOS 9.3.
  • We suspect that there may be yet more causes of this problem.

So, if you see this problem on a user device (one that hasn’t been talked to by Xcode) that’s running iOS 9.3 or later, please do file a bug report about it. Try to include the device system log in your bug report (I realise that can be tricky when dealing with customer devices; one option is to ask the customer to install Apple Configurator, which lets them view the system log). And if you do file a bug, please post your bug number, just for the record.

On behalf of Apple I’d like to thank everyone for their efforts in helping to track down this rather horrid issue. Share and Enjoy

https://forums.developer.apple.com/thread/4743#126088

关于ios - SecItemAdd 和 SecItemCopyMatching 返回错误代码 -34018 (errSecMissingEntitlement),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20344255/

相关文章:

linux - 不允许使用 RSA key

ios - 映射关注者/关注与 Restkit 的关系

objective-c - 如何让 NSView 不裁剪它的边界区域?

ios - 将 key 添加到 key 链项目

objective-c - 项目 “test.xcodeproj”被修改但被外部更改

cocoa - 目标-C : Programmatically set file privileges to current user

c - SecKeychainItemCopyContents 在私钥上出现段错误

ios - 如何使您的应用程序在 UIActivityViewcontroller 列出的事件中可用

ios - 删除导航栏按钮 subview

ios - 在 Swift 中使用 UIAlertController 的内存泄漏