Ios development-common methods of application data storage-plist access method, iosplist

Source: Internet
Author: User

Ios development-common methods of application data storage-plist access method, iosplist

[Note] reprinted, please indicate the source of blog Park-eat Tang sick Wukong http://www.cnblogs.com/hukezhu/

 

Common Data Storage Methods for ios apps include:

  • XML property list (plist) Archiving
  • Preference)
  • NSKeyedArchiver archive (NSCoding)
  • SQLite3
  • Core Data
Sandbox mechanism for ios apps

Before introducing these methods, we will first introduce the concept of using sandbox.

Ios app sandbox mechanism means that ios apps can only read files from the file system created for the app and cannot access the files from other places. This area is a sandbox, therefore, all non-code files must be stored here, such as icons, sounds, images, attribute lists, and text files. (This was previously the case for ios8. iOS8 has opened a new extension mechanism for several fixed system regions, to some extent, it can make up for the limitations of the sandbox mechanism of iOS on inter-Application Communication ).

Under the application sandbox mechanism, each application has its own storage space; the application cannot access the content of other buckets through its own walls; all data requested by the application must pass permission detection. If the request does not meet the conditions, the data will not be released.

The structure of the application sandbox is as follows:

 

Documents: stores the persistent data generated during application running. This directory is backed up when the iTunes synchronization device is used. For example, a game application can save a game archive to this directory.

  

Tmp: Save the temporary data required for running the application. After using the temporary data, delete the corresponding files from this directory. When the application is not running, the system may also clear files in the directory. The Library/Caches directory is not backed up when the iTunes synchronization device is used: stores the persistent data generated during the application running, and the directory is not backed up when the iTunes synchronization device is used. Generally, non-important data libraries/Preference with a large storage space and no backup is required: stores all Preference Settings of the application. The iOS Settings application searches for application Settings in this directory. When the iTunes synchronization device backs up this directory, the ios app sandbox directory common acquisition methods before introducing the plist file access method, there is another problem that needs to be introduced in advance and will be used later, this is how to obtain the directory used for sandbox. There are also several ways to obtain the root directory of the sandbox:
// Obtain the root directory NSString * home = NSHomeDirectory () of the application sandbox; // NSHomeDirectory () is a C Language Method

 

To obtain the file path of Documents:

    • Method 1:
      • First, obtain the root directory of the application sandbox, and use the String concatenation function to obtain the Documents directory (but this method is not recommended because the new operating system may change the directory name)
1 // Method 1: 2 // obtain the root directory of the current application sandbox 3 NSString * homePath = NSHomeDirectory (); 4 // splicing Path 5 NSString * docPath = [homePath stringByAppendingPathComponent: @ "Documents"];
    • Method 2
      • Use the NSSearchPathForDirectoriesInDomains function to obtain the Documents directory.
// Method 2 NSString * docPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0];

This NSSearchPathForDirectoriesInDomains function has a total of three parameters (note that an array is returned). The first parameter indicates the path of the obtained file (which can be selected by enumeration), which has been encapsulated by the system. the second parameter indicates finding the file from the user folder. The third parameter indicates whether the file is expanded during the search. this parameter is generally set to yes. Otherwise, the path may not be found, the obtained path. The system will change the previous path ~, For example:

    

 

Normally, if YES, the output is as follows:

  

 

Therefore, note that the third parameter of the NSSearchPathForDirectoriesInDomains function must be set to YES. note that this function returns an array, but there is only one folder in the sandbox on ios system, so you can take the 0th elements of the array.

In the mac system, there is a software simpholder2.app that can automatically find the root directory of the application sandbox, in the test is very convenient to use, you can download and use. (If you need to contact the 595632239@163.com)

 

 

To obtain the tmp directory of the sandbox:

  

// Obtain the tmp directory of the application sandbox NSString * tmp = NSTemporaryDirectory ();
 

  

  

How to obtain the caches directory of the application sandbox:

1 // obtain the tmp directory of the application sandbox 2 3 NSString * cachePath = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) [0]; 4 5 6 // You can also use the String concatenation method described above.

 

 

Method for obtaining the preference directory of the application sandbox:

Access the settings in this directory through the NSUserDefaults class

 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

 

 

 

Plist File Access Method

The property list is an XML file named plist.

If the object is of the NSString, NSDictionary, NSArray, NSData, or NSNumber type, you can use writeToFile: atomically: method to directly write the object to the property list file. I use a small demo to test the plist file access process. in the storyboard, drag two buttons, respectively, to save and read them: storage operation:
1-(IBAction) save {2 3 // you can obtain the root directory of the application sandbox in two ways. 4 // The first method is as follows: 5 //// obtain the root directory 6 of the current sandbox. // NSString * homePath = NSHomeDirectory (); 7 // splicing path 8 // NSString * docPath = [homePath stringByAppendingPathComponent: @ "Documents"]; 9 // 10 // you must specify the name of the file to be stored, still concatenate 11 // NSString * filePath = [docPath stringByAppendingPathComponent: @ "test. plist "]; 12 // NSLog (@" % @ ", filePath); 13 14 // Method 15 NSString * docPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; 16 // you must specify the name of the file to be stored. You can still concatenate 17 NSString * filePath = [docPath stringByAppendingPathComponent: @ "test. plist "]; 18 NSLog (@" % @ ", filePath); 19 // create an array (for example) 20 NSArray * array = @ [@" abc ", @ 123, @ "a-d-g"]; 21 22 // Save the array to the file 23 [array writeToFile: filePath atomically: YES]; 24 25 26 27 28}

 

 

Read operation:

  

1-(IBAction) read {2 3 // obtain the Documents directory 4 NSString * docPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; 5 // specify the name of the file to be stored, and use the string to splice 6 NSString * filePath = [docPath stringByAppendingPathComponent: @ "test. plist "]; 7 8 9 // use an array to accept data 10 NSArray * array = [NSArray arrayWithContentsOfFile: filePath]; 11 NSLog (@" % @ ", array ); 12 13}

 

I am using NSArray for test access, or use a dictionary. note that it is best not to use the plist file when accessing strings, because plist itself does not support strings (this can be done by creating a new plist file, after opening, it is found that there are only two options at the root node), and the string access method has expired.

 

Here, by the way, we will introduce the software simpholder2.app, which can automatically find the root directory of the application sandbox. After installation,

 

After clicking it, the sandbox of the current application is automatically opened, which is very convenient for testing.

 

  

 

Finally, the source code of the program is attached:

ViewController:

  

1 // 2 // ViewController. m 3 // 02-plist File Access 4 // 5 // Created by hukezhu on 15/6/5. 6 // Copyright (c) 2015 hukezhu. all rights reserved. 7 // 8 9 # import "ViewController. h "10 11 @ interface ViewController () 12-(IBAction) save; 13 14-(IBAction) read; 15 @ end16 17 @ implementation ViewController18 19-(void) viewDidLoad {20 [super viewDidLoad]; 21 // Do any additional setup after loading the view, typically from a nib.22} 23 24-(void) didreceivemorywarning {25 [super busy]; 26 // Dispose of any resources that can be recreated.27} 28 29 // you can use either of the following methods to obtain the root directory of the sandbox: 30 // Method 1: 31 32 33 34-(IBAction) save {35 36 // you can use either of the following methods to obtain the root directory of the application sandbox: 38 //// get the root directory 39 of the current sandbox // NSString * homePath = NSHomeDirectory (); 40 /// splicing path 41 // NSString * docPath = [homePath stringByAppendingPathComponent: @ "Documents"]; 42 // 43 /// you must specify the name of the file to be stored, still concatenate 44 // NSString * filePath = [docPath stringByAppendingPathComponent: @ "test. plist "]; 45 // NSLog (@" % @ ", filePath); 46 47 // method 48 NSString * docPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; 49 // you must specify the name of the file to be stored, and still splice 50 NSString * filePath = [docPath stringByAppendingPathComponent: @ "test. plist "]; 51 NSLog (@" % @ ", filePath); 52 // create an array (for example) 53 NSArray * array = @ [@" abc ", @ 123, @ "a-d-g"]; 54 55 // store the array in the file 56 [array writeToFile: filePath atomically: YES]; 57 58 59 60 61} 62 63-(IBAction) read {64 65 // obtain the Documents directory 66 NSString * docPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0]; 67 // specify the name of the file to be stored, and still concatenate 68 NSString * filePath = [docPath stringByAppendingPathComponent: @ "test. plist "]; 69 70 71 // use an array to accept data 72 NSArray * array = [NSArray arrayWithContentsOfFile: filePath]; 73 NSLog (@" % @ ", array ); 74 75} 76 @ endVIewController

  

This article only describes how to access a plist file, and how to access the remaining files.

 

 

 

 

According to the study, the machine organizes related knowledge points. If any errors are inappropriate, please correct them. Thank you.

 

 

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.