-25299 reason: 'Couldn't add the Keychain Item.'

时间:2024-12-29 19:07:26

今天在用苹果官方demo 提供的KeychainItemWrapper类时遇到-25299  reason: 'Couldn't add the Keychain Item.'错误,再4s上可以正常运行,但6上却崩溃

崩溃位置

 result = SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);

NSAssert( result == noErr, @"Couldn't add the Keychain Item." );

我调用的方法

  CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

        strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));

        [keychainItem setObject:strUUID forKey:(__bridge id)kSecValueData];

通过看国外的文章知道了方法(http://*.com/questions/4309110/error-saving-in-the-keychain-with-iphone-sdk

修改方法: 加上下面这行代码就能正常运行

[keychainItem setObject:@"MY_APP_CREDENTIALS" forKey:(id)kSecAttrService];

最后修改成代码为:

  [keychainItem setObject:@"MY_APP_CREDENTIALS" forKey:(id)kSecAttrService];

            CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);

        strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));

        [keychainItem setObject:strUUID forKey:(__bridge id)kSecValueData];

原因:

KeyChain内部以kSecAttrAccount 与kSecAttrService作为唯一钥匙串标识,两个都必须设置才行;