折腾:
期间,遇到问题:
用官网的:
swift版本:
只需要把:
下载到本地,加入到Xcode中,即可用:
let saveSuccessful: Bool = KeychainWrapper.setString(usernameTextField.text!, forKey: StrUsernameKey) print("saveSuccessful=\(saveSuccessful)") //saveSuccessful=true let retrievedString: String? = KeychainWrapper.stringForKey(StrUsernameKey) print("retrievedString=\(retrievedString)") //retrievedString=Optional("crifan@daryun.com") let removeSuccessful: Bool = KeychainWrapper.removeObjectForKey(StrUsernameKey) print("removeSuccessful=\(removeSuccessful)") //removeSuccessful=true let retrievedStringAfterDelete: String? = KeychainWrapper.stringForKey(StrUsernameKey) print("retrievedStringAfterDelete=\(retrievedStringAfterDelete)") //retrievedStringAfterDelete=nil
去以加密的方式去读取和保存密码了。
但是发现个问题:
如果用KeychainWrapper的话,下次程序再去用KeychainWrapper.stringForKey读取上次存储的值,是没有的。。。
swift KeychainItemWrapper remember password
去导入Security.framework
然后发现,此处是否添加 Security.framework,好像其实没啥影响。。。
后来才发现:
原来是自己此处的测试代码导致此问题的:
测试代码中,set后,retrieve后,调用了remove
-》导致下次直接去retrieve获取不到上次存的值了
-》去掉了remove后,在此调试一次,即可获得上次保存的值了:
// let saveSuccessful: Bool = KeychainWrapper.setString(usernameTextField.text!, forKey: StrUsernameKey) // print("saveSuccessful=\(saveSuccessful)") //saveSuccessful=true let retrievedString: String? = KeychainWrapper.stringForKey(StrUsernameKey) print("retrievedString=\(retrievedString)") //retrievedString=Optional("crifan@daryun.com") // let removeSuccessful: Bool = KeychainWrapper.removeObjectForKey(StrUsernameKey) // print("removeSuccessful=\(removeSuccessful)") //removeSuccessful=true // let retrievedStringAfterDelete: String? = KeychainWrapper.stringForKey(StrUsernameKey) // print("retrievedStringAfterDelete=\(retrievedStringAfterDelete)") //retrievedStringAfterDelete=nil