IOS project development practice-use user preference data API to store information

Source: Internet
Author: User

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:

.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.