繁体   English   中英

无法将值正确存储在钥匙串中

[英]Unable to store values in keychain correctly

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

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获取值。

可能是什么原因?

编辑:

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

我使用答案,原因是评论受到限制...我在新项目中尝试了以下代码,下载了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

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

其他键存储在任意大小的字典中,并放置在“密码” 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文件,但需要进行此更改(这是对苹果代码进行的唯一更改,而不是对其进行ARC化):

// 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

如您所见,您可以取消很多日志消息的注释,以查看实际运行情况。

此代码已在运输应用中使用。

如果您使用的是xCode 8.0请执行以下操作:

Project Navigator -> Select Target -> Capabilities 

启用KeyChain Sharing ON

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM