2

我正在使用以下内容将值存储在钥匙串中:

KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"suggest" accessGroup:nil];       
[keychain setObject:[object valueForKey:@"token"] forKey:(__bridge id)(kSecValueData)];
[keychain setObject:[object valueForKey:@"usr_id"] forKey:(__bridge id)(kSecAttrAccount)];

这是我检索值的代码:

KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"suggest" accessGroup:nil];   
NSLog(@"TOKEN:%@",[keychain objectForKey:(__bridge id)(kSecValueData)]);
NSLog(@"USER NAME:%@",[keychain objectForKey:(__bridge id)(kSecAttrAccount)]);

我还包括了安全框架。

我已经搜索了 stackoverflow 和 google,但无法在NSLog.

可能是什么原因?

编辑 :

如果有人有,也可以向我提供其他信息,以便使用钥匙串?

4

3 回答 3

3

我使用答案,因为评论受到限制......我从一个新项目中尝试了以下代码,下载 KeychainItemWrapper 并链接 Security.framework

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    {
        KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"suggest" accessGroup:nil];
        [keychain setObject:@"aaaa" forKey:(__bridge id)(kSecValueData)];
        [keychain setObject:@"bbbb" forKey:(__bridge id)(kSecAttrAccount)];
        NSLog(@"TOKEN:%@",[keychain objectForKey:(__bridge id)(kSecValueData)]);
        NSLog(@"USER NAME:%@",[keychain objectForKey:(__bridge id)(kSecAttrAccount)]);
    }

    {
        KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"suggest" accessGroup:nil];
        NSLog(@"TOKEN:%@",[keychain objectForKey:(__bridge id)(kSecValueData)]);
        NSLog(@"USER NAME:%@",[keychain objectForKey:(__bridge id)(kSecAttrAccount)]);
    }

    // Override point for customization after application launch.
    return YES;
}

一切都很完美。我想您需要提供更多代码才能找到答案。这是我的日志:

2013-11-19 17:11:08.378 test[3430:a0b] TOKEN:aaaa
2013-11-19 17:11:08.379 test[3430:a0b] USER NAME:bbbb
2013-11-19 17:11:08.380 test[3430:a0b] TOKEN:aaaa
2013-11-19 17:11:08.381 test[3430:a0b] USER NAME:bbbb
于 2013-11-19T16:16:00.987 回答
1

接下来是工作代码,可让您维护与主“键”(此处的电子邮件地址,可以是应用程序中的任何主标识符)相关的数据字典。

其他键存储在任意大小的字典中,并放置在“密码”keyChain 对象中。

首先,我把它放在我的 AppDelegate.h 文件的底部:

#define kEmail          @"email"
#define kPassword       @"pass"
#define kSomeOtherItemA @"a_item"
#define kSomeOtherItemB @"b_item"

@interface AppDelegate (Keychain)

- (NSDictionary *)keychainDictionary;                                   // current keyChain dictionary
- (void)eraseKeychain;                                                  // total wipe it
- (void)erasePassword;                                                  // wipe the password

@end

您可以在此处添加您认为合适的特殊方法。通过放置这是 AppDelegate.h 文件,导入该文件的任何其他类都可以使用这些方法。如果你想要它私有将它移动到你的 AppDelegate.m 文件

这在您的 AppDelegate.m 文件中,在顶部:

@interface AppDelegate (KeychainPrivate)

- (void)updateKeychainItem:(NSString *)item forKey:(NSString *)key; //the password, or itemA, or itemB
- (id)keychainItemForKey:(NSString *)key; // the password, itemA, itemB, ...

@end

这可以放在 AppDelegate.m 的底部或单独的文件中:

static char *kcIdentifier = "foo"; // use any string you want instead of "foo"

@implementation AppDelegate (Keychain)

- (KeychainItemWrapper *)keyChainItemWrapper
{
    static dispatch_once_t pred;
    static KeychainItemWrapper *kciw;

    dispatch_once(&pred, ^
        {
            kciw = [[KeychainItemWrapper alloc] initWithIdentifier:kcIdentifier accessGroup:nil];
        } );

    return kciw;
}


- (NSDictionary *)keychainDictionary
{
    NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithCapacity:3];
    KeychainItemWrapper *kcItem = [self keyChainItemWrapper];

    NSString *emailAddr = [kcItem objectForKey:(__bridge id)kSecAttrAccount];
    if([emailAddr length]) {
        NSData *data = [kcItem objectForKey:(__bridge id)kSecValueData];
        if([data length]) {
            NSKeyedUnarchiver *kua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            NSDictionary *dict = [kua decodeObject];
            [kua finishDecoding];
            [mDict addEntriesFromDictionary:dict];
        }
        mDict[kEmail] = emailAddr;  // last in case it got in the dictionary somehow
    }
    return mDict;
}

- (void)eraseKeychain
{
    KeychainItemWrapper *kcItem = [self keyChainItemWrapper];
    [kcItem resetKeychainItem];
}

- (void)erasePassword
{
    [self updateKeychainItem:@"" forKey:kPassword];
}

@end

@implementation AppDelegate (KeychainPrivate)

- (void)updateKeychainItem:(NSString *)item forKey:(NSString *)key
{
    KeychainItemWrapper *kcItem = [self keyChainItemWrapper];
    // NSLog(@"SET KC key=%@ ITEM %@", key, item);

    // NSLog(@"CURRENT KEYCHAIN: %@", [self keychainDictionary]);

    if([key isEqualToString:kEmail]) {
        NSString *emailAddr = [kcItem objectForKey:(__bridge id)kSecAttrAccount];
        if(![emailAddr isEqualToString:item]) {
            [kcItem setObject:item forKey:(__bridge id)kSecAttrAccount];
            // NSLog(@"SET KC Account ITEM %@", item);
        }
        // NSLog(@"KC Account ITEM %@ ALREADY SET", item);
    } else {
        NSData *data = [kcItem objectForKey:(__bridge id)kSecValueData];
        //NSLog(@"KC get DATA len=%d", [data length]);
        NSDictionary *dict;
        if([data length]) {
            NSKeyedUnarchiver *kua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            dict = [kua decodeObject];
            [kua finishDecoding];
        } else {
            dict = [NSDictionary dictionary];
        }
        //NSLog(@"KC OLD DICT %@", dict);

        if(![item isEqualToString:dict[key]]) {
            //NSLog(@"KC DATA NOT EQUAL");
            NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:dict];
            mDict[key] = item;

            NSMutableData *tmpData = [NSMutableData dataWithCapacity:256];
            NSKeyedArchiver *ka = [[NSKeyedArchiver alloc] initForWritingWithMutableData:tmpData];
            [ka encodeObject:mDict];
            [ka finishEncoding];
            //NSLog(@"KC ENCODE MDICT %@", mDict);

            [kcItem setObject:tmpData forKey:(__bridge id)kSecValueData];
            //NSLog(@"SET KC DATA LEN=%d", [tmpData length]);
        }
    }
    //NSLog(@"JUST UPDATED KEYCHAIN KEY=%@ val=%@ read dict back=%@", key, item, [self keychainDictionary]);
}

- (id)keychainItemForKey:(NSString *)key
{
    NSDictionary *dict = [self keychainDictionary];
    return dict[key];
}

@end

您还需要 KeyChainItemWrapper 类。你可以得到一个 ARCified 和稍微修改(以便处理字典)KeyChainItemWrapper 版本。您真的可以使用任何您想要的 KeyChainWrapper 文件,但需要进行此更改(这是除 ARCifying 之外对 Apple 代码的唯一更改):

// Default data for keychain item.
#ifndef PASSWORD_USES_DATA
    [keychainItemData setObject:@"" forKey:(__bridge id)kSecValueData];
#else
    [keychainItemData setObject:[NSData data] forKey:(__bridge id)kSecValueData];
#endif

如您所见,如果您愿意,可以取消注释许多日志消息以查看它的运行情况。

此代码用于运输应用程序。

于 2013-11-19T17:18:06.930 回答
0

如果您正在使用,xCode 8.0请执行以下操作:

Project Navigator -> Select Target -> Capabilities 

使能够KeyChain Sharing ON

在此处输入图像描述

于 2017-02-06T05:40:34.577 回答