【背景】
需要用swift语言实现,保存用户名和密码,都是字符串。
之前就看到了NSUserDefaults.standardUserDefaults:
//NSUserDefaults.standardUserDefaults().setValue("[email protected]", forKey: "userID") let b = NSUserDefaults.standardUserDefaults().stringForKey("userID") print("user defaults: " + "\(b)") var jabberID: String? = NSUserDefaults.standardUserDefaults().stringForKey("userID") print("jabberID=\(jabberID)") var myPassword: String? = NSUserDefaults.standardUserDefaults().stringForKey("userPassword") print("myPassword=\(myPassword)") var server: String? = NSUserDefaults.standardUserDefaults().stringForKey("loginServer") print("server=\(server)") if server != nil{ loginServer = server! } curXmppStream!.hostName = "localhost"
现在需要去搞懂,到底如何使用,具体是什么含义。
【折腾过程】
1.搜:
swift store username password
参考:
NSUserDefaults — A Swift Introduction — Coding Explorer Blog
Securing iOS User Data: Keychain, Touch ID, and 1Password
2.搜:
swift nsuserdefaults
参考:
NSUserDefaults Class Reference
Reading and writing basics: NSUserDefaults – a free Hacking with Swift tutorial
About Preferences and Settings
About the User Defaults System
iphone – iOS: How to store username/password within an app? – Stack Overflow
3.搜:
NSURLCredential vs KeychainItemWrapper
swift NSURLCredential
参考:
Using the Keychain to store passwords on iOS | b2cloud
官网的:
别人改为Swift的版本,有两个:
jrendel/SwiftKeychainWrapper · GitHub
macostea/KeychainItemWrapper.swift · GitHub
【总结】
Cocoa preferences system==user defaults system,用于存储用户设置/配置,主要是存在本地
也可以存在iCloud云端(的键值存储key-value store)
-》
保存用户配置,有两种:
- user defaults system
- key-value store
都只是用于存储简单数据
-》存在一个属性列表中
想要存复杂的数据的话,可以将其编码为NSData再存储
存取本地的用户配置的话:
- iOS用:NSUserDefaults
- OS X用:NSUserDefaults或NSUserDefaultsController
最后实例代码是:
(1)不推荐的做法:用明文保存用户名和密码,可以用:
NSUserDefaults.standardUserDefaults().setValue(usernameTextField.text, forKey: StrUsernameKey) NSUserDefaults.standardUserDefaults().setValue(passwordTextField.text, forKey: StrPasswordKey) let storedUsername = NSUserDefaults.standardUserDefaults().stringForKey(StrUsernameKey) let storedPassword = NSUserDefaults.standardUserDefaults().stringForKey(StrPasswordKey) print("storedUsername=\(storedUsername)") //Optional("xxxxx") print("storedPassword=\(storedPassword)") //Optional("yyyyy")
(2)推荐的做法:用加密的方式保存用户名和密码
想要加密方式保存的话,可以:
用官网的:
被别人改为Swift语言了:
jrendel/SwiftKeychainWrapper · GitHub
该库的使用方法也很简单:
只需要把:
下载到本地,加入到Xcode中,即可用:
let StrUsernameKey:String = "username" let StrPasswordKey:String = "password" 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("xxx") let removeSuccessful: Bool = KeychainWrapper.removeObjectForKey(StrUsernameKey) print("removeSuccessful=\(removeSuccessful)") //removeSuccessful=true let retrievedStringAfterDelete: String? = KeychainWrapper.stringForKey(StrUsernameKey) print("retrievedStringAfterDelete=\(retrievedStringAfterDelete)") //retrievedStringAfterDelete=nil
去以加密的方式去读取和保存密码了。
转载请注明:在路上 » 【已解决】用swift保存用户名和密码