+(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);
}
Idali你好是有什麼錯在我的代碼! –
我開始用你的代碼,但得到與缺少參數(-50)也需要使用可變dictionnary錯誤....我一步步的一個改變你dictionnary初始化。 – Idali
非常感謝!你有在設備上測試過嗎?因爲我沒有設備。 –