iOS保存用户帐号及密码

之前一直用NSUserDefaults在登录的时候来保存用户名和密码,现在换成另外一种方式,记录一下,以免以后用到能够直接command+v

第一种方法–NSUserDefaults
创建一个继承NSObject的类,在.h里面添加方法

+(void)saveUserName:(NSString *)userName;//保存用户名
+(NSString *)getUserName;//获取用户名
+(void)savePassWD:(NSString *)passWD;//保存用户密码
+(NSString *)passWD;//获取用户密码

在.m里面实现方法

//储存用户名
+(void)saveUserName:(NSString *)userName
{
    NSUserDefaults *name = [NSUserDefaults standardUserDefaults];
    [name setObject:userName forKey:@"userName"];
}
+(NSString *)getUserName
{
    NSUserDefaults *name = [NSUserDefaults standardUserDefaults];
    NSString *str = [name objectForKey:@"userName"];
    return str;
}
//储存用户密码
+(void)savePassWD:(NSString *)passWD
{
    NSUserDefaults *passWord = [NSUserDefaults standardUserDefaults];
    [passWord setObject:passWD forKey:@"passWord"];
}
+(NSString *)passWD
{
    NSUserDefaults *passWord = [NSUserDefaults standardUserDefaults];
    NSString *str = [passWord objectForKey:@"passWord"];
    return str;
}

第二种方法–Keychain
创建一个继承NSObject的类,在.h里面添加方法

导入头文件 #import 
+(void)addKeychainData:(id)data forKey:(NSString *)key;//保存
+(id)getKeychainDataForKey:(NSString *)key;//获取
+(void)deleteKeychainDataForKey:(NSString *)key;//删除

在.m里面实现方法

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (__bridge id)kSecClassGenericPassword,(id)kSecClass,// 标识符(kSecAttrGeneric通常值位密码)
            service, (__bridge id)kSecAttrService,// 服务(kSecAttrService)
            service, (__bridge id)kSecAttrAccount,// 账户(kSecAttrAccount)
            (__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,// kSecAttrAccessiblein变量用来指定这个应用何时需要访问这个数据
            nil];
}
/**
 储存用户账号
 */
+(void)addKeychainData:(id)data forKey:(NSString *)key
{
    // 获取查询字典
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
    // 在删除之前先删除旧数据
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
    // 添加新的数据到字典
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
    // 将数据字典添加到钥匙串
    SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);

}
+(id)getKeychainDataForKey:(NSString *)key
{
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
    [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
        } @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@",key,e);
        } @finally {

        }
    }

    if (keyData) {
        CFRelease(keyData);
    }

    return ret;
}
+(void)deleteKeychainDataForKey:(NSString *)key
{
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}

以后开发中用到的小tool,都会依次更新,,欢迎大家给出更好的意见!简书传送门

你可能感兴趣的:(iOS保存用户帐号及密码)