无法添加钥匙串项目。使用 KeychainItemWrapper 更改标识符后出现错误 - 25299?

mag*_*527 3 keychain ios keychainitemwrapper

我想使用 KeychainItemWrapper 将 UUID 保存在钥匙串中,因此我在MyKeychainManager.m中添加以下方法:

#define keychain_idenentify @"com.myapp.bundle1"

+ (void)saveUUID:(NSString *)UUID{
  if([MyKeychainManager getUUID].length > 0) {
    return;
  }
  KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc]initWithIdentifier:keychain_idenentify accessGroup:nil];
  [keychain setObject:UUID forKey:(__bridge id)kSecAttrLabel];
}

+ (NSString *)getUUID {
  KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc]initWithIdentifier:keychain_idenentify accessGroup:nil];

  NSString *uuidString = [keychain objectForKey:(__bridge id)kSecAttrLabel];
  return uuidString;
}
Run Code Online (Sandbox Code Playgroud)

但是当我将keychain_identify更改为com.otherApp.bundle后,它崩溃了

结果 = SecItemAdd((CFDictionaryRef)[selfdictionaryToSecItemFormat:keychainItemData], NULL); NSAssert( result == noErr, @"无法添加钥匙串项目。" );

错误是错误 - 25299

这是为什么以及如何解决这个问题?我应该选择哪个,例如kSecAttrLabel?我将其更改为kSecAttrService它很有趣,但我不知道是否还有其他潜在的错误。

任何帮助将不胜感激。

swi*_*ynx 5

此错误意味着具有此标识符的项目已存在 ( https://osstatus.com/search/results?platform=all&framework=all&search=-25299 )。尝试从钥匙串中删除它,然后您的代码应该可以工作。