我可以将Realm用于所有数据存储,还是应该使用NSUserDefaults存储用户名/密码?

时间:2017-08-17 12:45:38

标签: ios swift realm nsuserdefaults

我有一个用户名/密码登录的应用程序。登录后,用户应保持登录状态,直到他们退出为止,即即使用户没有连接,用户仍会保持登录状态。

目前我正在对用户进行身份验证,但我无法让他们登录。我应该将用户详细信息存储在NSUserDefaults中以便在启动时登录,还是有办法让用户只使用Realm登录?

提前致谢!

SyncUser.logIn(with: userCredentials, server: (url! as URL)) { user, error in
    guard user != nil else {
        // Handles error
    }
    DispatchQueue.main.async {
        let configuration = Realm.Configuration(
            syncConfiguration: SyncConfiguration(user: user!, realmURL: URL(string: "realm://127.0.0.1:9080/~/realmtasks")!)
        )
        Realm.Configuration.defaultConfiguration = configuration

        self.performSegue(withIdentifier: "logInSegue", sender: self)
    }
}

更新:也许我的问题的答案是Apple Keychain?

2 个答案:

答案 0 :(得分:2)

您不应该在NSUserDefault内存储密码或任何敏感信息,通过使用简单的文件管理器可以读取存储在其中的所有数据,这实际上是不安全的。你甚至不需要数据库或者那种。存储敏感信息的正确位置是您设备的钥匙串 github上有很多lib可以帮助你使用它 请注意,即使删除了应用程序,您在钥匙串中保存的内容也会保留。

答案 1 :(得分:1)

您可以考虑将其保存在钥匙串中。只需创建一个 Objective-c 文件,然后将其导入项目的 Bridging-header-file

·H

#import <Foundation/Foundation.h>

@interface KeychainUserPass : NSObject

+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)delete:(NSString *)service;

@end

的.m

#import "KeychainUserPass.h"

@implementation KeychainUserPass

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {

    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
            service, (__bridge id)kSecAttrService,
            service, (__bridge id)kSecAttrAccount,
            (__bridge id)kSecAttrAccessibleAfterFirstUnlock, (__bridge id)kSecAttrAccessible,
            nil];
}

+ (void)save:(NSString *)service data:(id)data {

    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
    SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
}

+ (id)load:(NSString *)service {

    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    [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: %@", service, e);
        }
        @finally {}
    }
    if (keyData) CFRelease(keyData);
    return ret;
}

+ (void)delete:(NSString *)service {

    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}


@end

<强>用法

保存:KeychainUserPass.save("email", data: self.YOUR_TEXT_FIELD.text!)

加载:YOUR_STRING = KeychainUserPass.load("email") as? String

删除:KeychainUserPass.delete("email")