iOS 本地保存用戶名密碼等敏感信息刨晴,使用plist文件存儲(chǔ)或者NSUserdefaults并不安全卸留,需要進(jìn)行加密空镜。蘋果keychain不僅僅申請(qǐng)開發(fā)證書使用染厅,也是蘋果給出的保存密碼的最佳解決方案桑腮。
我們使用SFHFKeychainUtils第三方庫(kù)來實(shí)現(xiàn)保存泉哈、讀取和刪除用戶名密碼操作。
SFHFKeychainUtils下載地址:https://github.com/ldandersen/scifihifi-iphone/tree/master/security破讨。
新建工程丛晦,把下載的SFHFKeychainUtils.h和SFHFKeychainUtils.m文件添加到工程中,由于下載的SFHFKeychainUtils不支持ARC提陶,所以要在TARGETS->Build Phases->Complie Resources 中的SFHFKeychainUtils.m添加 -fno-objc-arc的Compiler Flags烫沙。最后添加Security.framework框架。
//保存用戶名密碼
?NSString*serviceName =@"com.XXXX.XXXX";??
?[SFHFKeychainUtils storeUsername:self.mobileTextField.text andPassword:self.passwordTextField.text forServiceName:serviceName updateExisting:YES error:nil];
//讀取用戶名密碼
NSString *password = [SFHFKeychainUtils getPasswordForUsername:LOGIN_USER_MOBILE andServiceName:serviceName error:nil];
//刪除用戶名密碼
[SFHFKeychainUtils deleteItemForUsername:LOGIN_USER_MOBILE andServiceName:serviceName error:nil];
這樣我們的APP就更加安全了隙笆!