How iOS app data is stored (XML attribute list-plist)

Source: Internet
Author: User
Tags xml attribute

How iOS app data is stored (XML attribute list-plist)

One, the iOS application common data storage way

1.plist (XML attribute list archive) 2. Preferences 3.NSKeydeArchiver Archive (store custom objects) 4.sqlite3 (database, relational database, cannot directly store objects, to write some database statements, to disassemble the object storage) 5.Core Data (Object-based database, block internal links) second, the application of sand box

Every iOS app has a? Sandbox (the application sandbox is the file system directory), which is isolated from other file systems. You must stay in your own sandbox, other apps cannot access the sandbox (hint: already open in IOS8)

The file system of the sandbox should be recorded as shown (assuming the name of the application is called layer)

Simulator should use the root path of the sandbox at: (Apple is? Username, 7.0 is emulator version)/users/apple/library/application Support/iphone simulator/7.0/applications

Three, the application of sandbox structure analysis

Package: (layer in) contains all the resource files and executable files

Documents: Save the data that needs to be persisted at run time, which is backed up when itunes synchronizes the device. For example, a game app can save a game archive in that directory

TMP: Save the temporary data that is required at run time, and then delete the corresponding file from the directory when you are finished. When the app is not running, the system may also purge files in that directory. itunes does not back up this directory when syncing the device

Library/caches: Save when the app runs? data that needs to be persisted, itunes does not back up the directory when syncing the device. Non-critical data with large storage volumes and no backup required

Library/preference: Save all of your app's preferences, iOS settings (settings) should be in this record to find the settings information. This directory is backed up when itunes syncs the device

Iv. application of sandbox common access methods

Sandbox root directory: nsstring *home = Nshomedirectory (); Documents: (2 ways)

Stitching the "Documents" string with the sandbox root
NSString *home = Nshomedirectory ();
NSString *documents = [Home stringbyappendingpathcomponent:@ "documents"]; Not recommended because a new version of the operating system may modify the directory name

Philip? Nssearchpathfordirectoriesindomains function
Nsuserdomainmask representative from the user folder to find
YES represents the wavy character "~" in the expanded path
Nsarray *array = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, NO); In iOS, only one directory matches the parameters passed in, so there is only one element in the collection

NSString *documents = [array objectatindex:0];

Tmp:nsstring *tmp = Nstemporarydirectory ();

Library/caches: (2 ways similar to documents)

Stitching the "Caches" string with the sandbox root

Philip? Nssearchpathfordirectoriesindomains function (change the function's 2nd parameter to: nscachesdirectory)

Library/preference: Access to Settings information in this directory through the Nsuserdefaults class

The corresponding code:

 1 #import "NJViewController.h" 2 #import "NJPerson.h" 3 4 @interface Njviewcontroller () 5-(ibaction) Savedatabtnclick: (ID) sender; 6-(Ibaction) Readdatabtnclick: (ID) sender;     7 8 @end 9 @implementation NJViewController11/**12 * Click the save button */14-(ibaction) Savedatabtnclick: (ID) Sender {15 //YouTube practices//NSString *path = @ "/users/apple/library/application support/iphone simulator/7.1/application S/a6d53e11-ddf0-4392-b2d4-fe77a96888a6/documents/abc.plist "; 18 19//Get the application root directory nsstring *home = Nshomedirect Ory (); 21 22//Not recommended write/23//nsstring *path = [Home stringbyappendingstring:@ "/documents"];24//Do not advise Documents to write      Dead//nsstring *path = [Home stringbyappendingpathcomponent:@ "Documents"];26//Nsuserdomainmask in the user directory find 28 YES on behalf of the user directory ~29//nsdocumentdirectory Find Documents folder 30//recommended to use the following methods to dynamically obtain the nsstring *doc = [Nssearchpath Fordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];32//SpellAccess file path NSString *path = [Doc stringbyappendingpathcomponent:@ "Abc.plist"];34 NSLog (@ "%@", Path); 35 36 37  Nsarray *arr = @[@ "Lnj", @ "];38//[arr writetofile:path atomically:yes];39"//nsdictionary *dict =      @{@ "name": @ "LNJ" @ "Age": @ "28"};41//Call WriteToFile Write data to File//[Dict Writetofile:path atomically:yes];43 44 /*45 plist can only store some of the regular classes that the system comes with, that is, objects that have writetofile methods may use plist to save data 46 string/dictionary/Data/nsnumber/nsdata ... 47 */48 49//Custom object cannot be saved to plist njperson *p = [[Njperson alloc] init];51 p.name [email protect Ed] "Lnj"; nsdictionary *dict = @{@ "person": @ "abc"};54 [dict writetofile:path atomically:yes];55}56/**5 7 * Click the Read button */59-(ibaction) Readdatabtnclick: (ID) Sender {nsstring *doc = [nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];61 nsstring *path = [Doc Stringbyappendingpathcomponen t:@ "Abc.plist"]63; 64//Read Data 65 Nsdictionary *dict = [nsdictionary dictionarywithcontentsoffile:path];66 NSLog (@ "%@", dict);}68 @end 

V. Attribute list

The attribute list is an XML-formatted file that expands to the name plist

If the object is NSString, Nsdictionary, Nsarray, NSData, NSNumber, and so on, you can use the writetofile:atomically:? method to write the object directly to the property list file

How iOS app data is stored (XML attribute list-plist)

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.