如何使用apple docs中的keychain示例

时间:2013-05-08 23:45:17

标签: ios objective-c nsstring keychain

我正在尝试将字符串值存储并检索到我的钥匙串,我一直在使用找到here的示例,但是当我调用mySetObject方法时,我的应用程序崩溃了。

这就是我用来访问keychain类方法mySetObject

的代码
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Test keychain Wrapper for future use through out connectionClasses
    KeychainWrapper *keychainWrapper = [[KeychainWrapper alloc] init];

    // set key
    [keychainWrapper mySetObject:@"myObj" forKey:@"entry1"]; // this is where my app falls over

    // read key

    NSString *myAwesomeID = [keychainWrapper myObjectForKey:@"myObj"];
    NSLog(@"%@", myAwesomeID);

//..

我在输出中收到的错误如下

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'
*** First throw call stack:

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

此包装器(KeychainItemWrapper)类似于NSDictionary,您可以设置键的值,但是您无法选择键。请参阅blog posting

所以不能这样做:

// set key
[keychainWrapper mySetObject:@"myObj" forKey:@"entry1"]; // this is where my app falls over