IOS project development practice-use user preference data API to store information
Anyone who has developed Android knows that Content Provider, one of the four main components in Android, is a lightweight data storage component that stores simple configuration information or user information. For example, remember the user name and password, so that the user can not enter the password the next time the application is opened for easy operation. In iOS, Apple also provides APIs with similar functions-user preference data, which is used to store simple user configuration information, which is practical, convenient, and efficient. The specific implementation steps are as follows:
(1) drag an input text box TextView and save Button in Main. storyboard to store the content in the text box and display it the next time you open the application. Bind TextView to Outlet, and bind the button to Action.
(2) The implementation in the Code is as follows:
Import UIKitclass ViewController: UIViewController {@ IBOutlet weak var inputText: UITextView! Var ud: NSUserDefaults! // Declare user preferences; override func viewDidLoad () {super. viewDidLoad () ud = NSUserDefaults. standardUserDefaults () // The returned AnyObject type can be empty. Therefore, if let is determined. if it is not empty, it is filled in the text box. if let uudd: AnyObject = ud. objectForKey (data) {inputText. text = uudd! String }}@ IBAction func saveButton (sender: UIButton) {ud. setObject (inputText. text, forKey: data) // click the button to store the content in the text box. The key is "data" println (Saved)} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}
(3) The program runs as follows:
First, save a piece of text:
.
Then exit the program and re-open the program. The text you just found can be displayed on the interface:
.