检查钥匙链中是否存在特定用户名如果​​不存在,则使用密码 ios objective-c 存储新用户

Checking Particular username exits in KeyChain or not If not then store New User with password ios objcetive-c

我需要将用户名和密码值存储到 keychain ,下次如果我想存储新的用户名和密码,如果用户名已经存在,它会显示消息,但如果用户名不同,它应该存储它。 以下是我的代码 我能够存储但不知道如何检查特定的用户名。

+(bool)CheckValuesInKeyChainWithUsername:(NSString*)username
 {

NSDictionary* dictSearch = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword),  kSecClass,  kCFBooleanTrue, kSecReturnAttributes, kCFBooleanTrue, kSecReturnData, nil];
NSDictionary* found = nil;
CFDictionaryRef foundCF;
OSStatus errOS = SecItemCopyMatching((__bridge CFDictionaryRef) dictSearch, (CFTypeRef*)&foundCF);
NSLog(@"%d",(int)errOS);
found = (__bridge NSDictionary*)(foundCF);
if (!found)
{
    return false;
}
return true;
}


 +(void)SaveUsername:(NSString*)user withPassword:(NSString*)pass 
 {

// Create dictionary of parameters to add
NSData* passwordData = [pass dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword), kSecClass, passwordData, kSecValueData, user, kSecAttrAccount, nil];

// Try to save to keychain
OSStatus errOS = SecItemAdd((__bridge CFDictionaryRef) dict, NULL);
NSLog(@"Error Code: %d", (int)errOS);

}

这里是经过测试的代码。 您需要将字典设置为可变的,还要检查您的情况是否存在无效参数 (errOS == errSecPara)。

    +(bool)CheckValuesInKeyChainWithUsername:(NSString*)username
{

    NSMutableDictionary* dictSearch = [[NSMutableDictionary alloc]init];

    [dictSearch setObject:(__bridge id)kSecClassGenericPassword
                              forKey:(__bridge id)kSecClass];

    [dictSearch setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];

    [dictSearch setObject:(__bridge id)kCFBooleanTrue
                              forKey:(__bridge id)kSecReturnAttributes];


    [dictSearch setObject:username forKey:(__bridge id)kSecAttrAccount];

    /*
    Unique string used to identify the keychain item: 
     //static const UInt8 kKeychainItemIdentifier[]    = "com.company.MyApp";


    NSData *keychainItemID = [NSData dataWithBytes:kKeychainItemIdentifier
                                            length:strlen((const char *)kKeychainItemIdentifier)];

    [dictSearch setObject:keychainItemID forKey:(__bridge id)kSecAttrGeneric];
     */

    NSMutableDictionary* found = nil;
    CFMutableDictionaryRef foundCF = nil;
    OSStatus errOS = noErr;
    errOS = SecItemCopyMatching((__bridge CFDictionaryRef) dictSearch, (CFTypeRef*)&foundCF);
    NSLog(@"%d",(int)errOS);


    if (errOS == errSecItemNotFound)
    {
        return false;

    }else if (errOS == noErr)
    {
        found = (__bridge NSMutableDictionary*)(foundCF);
        return true;
    }

    return false;
}
+(bool)CheckUserNameExistsOrNot
{
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword),  kSecClass, serviceName, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, kCFBooleanTrue, kSecReturnData, nil];

// Look up in the keychain
CFDictionaryRef foundCF;
OSStatus err = SecItemCopyMatching((__bridge CFDictionaryRef) dict, (CFTypeRef*)&foundCF);
if (err == errSecItemNotFound)
{
    return  false;
}

return true;

}
+(bool)CheckIfInputsArePerfect:(NSString*)username ansPassword:(NSString*)password
{
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword),  kSecClass, serviceName, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, kCFBooleanTrue, kSecReturnData, nil];

// Look up in the keychain
NSDictionary* found = nil;
CFDictionaryRef foundCF;
OSStatus err = SecItemCopyMatching((__bridge CFDictionaryRef) dict, (CFTypeRef*)&foundCF);
if (err == errSecItemNotFound)
{
    return  false;
}

found = (__bridge NSDictionary*)(foundCF);

// Found
NSString* user = (NSString*) [found objectForKey:(__bridge id)(kSecAttrAccount)];
NSString* pass = [[NSString alloc] initWithData:[found objectForKey:(__bridge id)(kSecValueData)] encoding:NSUTF8StringEncoding];

NSLog(@"User Name :%@ and password %@",user,pass);
if([user isEqualToString:username] && [pass isEqualToString:password]){
    return  true;
}
return false;
 }

 +(void)RemoveKeyChain
 {
// Create dictionary of search parameters
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword),  kSecClass, serviceName, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, kCFBooleanTrue, kSecReturnData, nil];

// Remove any old values from the keychain
OSStatus err = SecItemDelete((__bridge CFDictionaryRef) dict);
NSLog(@"%d",(int)err);
}
+(void)SaveUsername:(NSString*)user withPassword:(NSString*)pass {


NSDictionary* dict ;//= [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword),  kSecClass, serviceName, kSecAttrServer, kCFBooleanTrue, kSecReturnAttributes, nil];

// Remove any old values from the keychain
OSStatus err;// = SecItemDelete((__bridge CFDictionaryRef) dict);

// Create dictionary of parameters to add
NSData* passwordData = [pass dataUsingEncoding:NSUTF8StringEncoding];
dict = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)(kSecClassInternetPassword), kSecClass, serviceName, kSecAttrServer, passwordData, kSecValueData, user, kSecAttrAccount, nil];

// Try to save to keychain
err = SecItemAdd((__bridge CFDictionaryRef) dict, NULL);                                           
NSLog(@"Error Code: %d", (int)err);

}