如何在密钥链中存储字符串数组?

时间:2022-06-25 15:43:39

i have followed "GenericKeychain" sample app from apple & already able to write a string into keychain & also able to read it from keychain.

我使用了苹果公司的“通用钥匙链”示例应用程序,已经能够将字符串写入钥匙链,也能够从钥匙链读取。

But i am wondering how can i store an array of string inside keychain, Is it possible ? How ?

但是我想知道如何在keychain中存储一个字符串数组,可能吗?如何?

1 个解决方案

#1


6  

You can only store objects of type CFDataRef (which is toll-free bridged to NSData) in a keychain.

您只能在keychain中存储CFDataRef类型的对象(这是免费的桥接到NSData)。

To store an array of strings you would typically use property list serialization:

要存储字符串数组,通常使用属性列表序列化:

NSArray *myArray = @[@"my", @"strings"];
NSData *data = [NSPropertyListSerialization dataWithPropertyList:myArray format: NSPropertyListBinaryFormat_v1_0 options:0 error:NULL];

Now you can put data into the keychain.

现在可以将数据放入密钥链中。

#1


6  

You can only store objects of type CFDataRef (which is toll-free bridged to NSData) in a keychain.

您只能在keychain中存储CFDataRef类型的对象(这是免费的桥接到NSData)。

To store an array of strings you would typically use property list serialization:

要存储字符串数组,通常使用属性列表序列化:

NSArray *myArray = @[@"my", @"strings"];
NSData *data = [NSPropertyListSerialization dataWithPropertyList:myArray format: NSPropertyListBinaryFormat_v1_0 options:0 error:NULL];

Now you can put data into the keychain.

现在可以将数据放入密钥链中。