iOS KeychainItemWrapper not updating

前端 未结 2 1668
时光取名叫无心
时光取名叫无心 2020-12-14 12:33

I just found an interesting problem with my app. In the app I am saving the user\'s user name and password to the keychain.

keychainWrapper = [[KeychainItemW         


        
相关标签:
2条回答
  • 2020-12-14 13:02

    When you include

    keychainWrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"MyLoginPassword" accessGroup:nil];
    
    [keychainWrapper setObject:usernameField.text forKey:(id)kSecAttrAccount];
    [keychainWrapper setObject:passwordField.text forKey:(id)kSecValueData];
    

    you also had to include

    [keychainWrapper setObject:@"Myappstring" forKey: (id)kSecAttrService];
    

    Or I get a "SIGABRT" error. (Myappstring) is a string defining your application.

    Maybe I'm missing something, this should be done at least once.

    0 讨论(0)
  • 2020-12-14 13:09

    I also had this problem, and it took me forever to figure out

    There is a version of "KeychainWrapper" floating around that has it's SecItemUpdate within an NSAssert (among other things).

    Whoever did this is a moron!, when building for release/distribution every NSAssert is nullified, meaning that code doesn't even get run.

    For example:

    NSAssert(SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck), @"Couldn't update the Keychain Item." );
    

    Needs to become

    OSStatus status = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck);
    NSAssert(status == noErr, @"Couldn't update the Keychain Item." );
    

    Notice how the actual SecItemUpdate is moved outside the NSAssert, and instead the result is checked

    Important note: Attempting to update a value for kSecValueData, without also specifying a value for kSecAttrAccount, will cause the assertion to fail as well. So, if your intent is to store a single string of sensitive data (such as a list of credit card numbers), be sure to store some "account name" text in the kSecAttrAccount attribute, like so:

    static NSString* kCardListXML = @"cardListXML";
    static NSString* cardListAccountName = @"cardListAccount";
    
    -(void)setCardListXML:(NSString*)xml {
      KeychainItemWrapper* wrapper =
        [[KeychainItemWrapper alloc] initWithIdentifier:kCardListXML accessGroup:nil];
      [wrapper setObject:cardListAccountName forKey:(id)CFBridgingRelease(kSecAttrAccount)];
      [wrapper setObject:xml forKey:(id)CFBridgingRelease(kSecValueData)];
    }    
    
    -(NSString*)getCardListXML {
      KeychainItemWrapper* wrapper =
        [[KeychainItemWrapper alloc] initWithIdentifier:kCardListXML accessGroup:nil];
      [wrapper setObject:cardListAccountName forKey:(id)CFBridgingRelease(kSecAttrAccount)];
      return [wrapper objectForKey:CFBridgingRelease(kSecValueData)];
    }
    
    0 讨论(0)
提交回复
热议问题