Ios_sn_ Sandbox file operation and location

Source: Internet
Author: User

Reprint: http://blog.csdn.net/hello_hwc/article/details/44916909

The structure of the sandbox is as follows

One access bundle

Note that bundles are read-only and cannot be written
Create a plist file

Then write a pair of key-value to the plist

Then access the bundle

1 nsurl * fileinbundle =  [[NSBundle Mainbundle] Urlforresource:@ "demoplist" Withextension:@ "plist"]; 2 nsdictionary * dictionary = [nsdictionary dictionarywithcontentsofurl:fileinbundle]; 3 NSLog (@ "%@", dictionary.description);

Content of output plist

2015-04-07 10:13:14.135 SandboxDemo[669:58884] { url = "blog.csdn.net/hello_hwc";}
Two access document

First of all, two functions are introduced.

 1// search for directory under Domainmask, return an array of type NSString

2 Nsarray * nssearchpathfordirectoriesindomains (nssearchpathdirectory directory, Nssearchpathdomainmask Domainmask, BOOL Expandtilde);

1 // search for directory under Domainmask, return an array of type Nsurl

2 -(Nsarray *) Urlsfordirectory: (nssearchpathdirectory) directory 3 indomains: ( Nssearchpathdomainmask) Domainmask

2.1 We visit the document and see what's going on.

 1 nsstring * Documentsurl =

2 NSLog (@ "%@", Tostoreurl);

Output

2015-04-07  10: 38:36.082 SandboxDemo[921:79142"/users/huangwenchen/ library/developer/CoreSimulator/devices/158d1ce6-0444- 454b-b248-798726D6BC40/data/containers/data /application/1cd56d62-9370-4ff7-84e3-888a4fd73071/Documents  

Open the sandbox of the virtual machine to see what is currently available, you can see that the path to the current document is empty

2.2 Write something to the document directory

1NSString * Documentsurl =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) firstobject];2NSString * Tostoreurl = [Documentsurl stringbyappendingpathcomponent:@"Demo.txt"];3NSString * Text =@"Welcome to my csdn BLOG;BLOG.CSDN.NET/HELLO_HWC";4BOOL success =[text Writetofile:tostoreurl atomically:yes encoding:nsutf8stringencoding Error:nil];5NSLog (@"%d", success);

We then looked at the documents directory and successfully wrote the file

2.3 Read the document directory

1    NSString * Documentsurl = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) Firstobject]; 2     NSString * Tostoreurl = [Documentsurl stringbyappendingpathcomponent:@ "demo.txt"]; 3     string = [[NSString alloc] Initwithcontentsoffile:tostoreurl encoding:nsutf8stringencoding error:nil ]; 4     NSLog (@ "%@",string);

Output

2015-04-07 11:19:06.264 SandboxDemo[1128:90655] welcome to my csdn blog;blog.csdn.net/hello_hwc
Three Library

3.1
First look at the contents of the default library

Then, we try to write a file

     NSString * Documentsurl = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) firstObject ];    NSString * Tostoreurl = [Documentsurl stringbyappendingpathcomponent:@ "Demo.txt"];    NSString * Text = @ "This file was in library dir";    BOOL success = [text Writetofile:tostoreurl atomically:yes encoding:nsutf8stringencoding Error:nil];    NSLog (@ "%d", success);

  

Look at the catalogue again.

Read, code and documents very similar

NSString * Documentsurl = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) firstObject ];    NSString * Tostoreurl = [Documentsurl stringbyappendingpathcomponent:@ "Demo.txt"];    NSString * string = [[NSString alloc] Initwithcontentsoffile:tostoreurl encoding:nsutf8stringencoding Error:nil];    NSLog (@ "%@", string);

  

3.2 Let's see what directory nsuserdefaults is stored in.
Before writing to Nsuserdefaulsts

Write with the following code

Nsuserdefaults * defaults = [Nsuserdefaults standarduserdefaults];    [Defaults setfloat:1.23 forkey:@ "Float_key"];    [Defaults synchronize];

  

After writing

It's not hard to see, it's written in the library/preferences/directory.

Four Access Temp

the files in the temp directory, the app should be deleted when not needed, the system may also clean up the directory when the app is not running.
The default

Write Temp

NSString * tempdir = Nstemporarydirectory ();    NSString * Filedir = [tempdir stringbyappendingpathcomponent:@ "Temp.txt"];    NSString * message = @ "This was in temp dir";    BOOL success = [Message Writetofile:filedir atomically:yes encoding:nsutf8stringencoding Error:nil];    NSLog (@ "%d", success);

  

Clean Temp

Nsfilemanager * defaultmanager= [Nsfilemanager Defaultmanager];     * TempDir = [Defaultmanager contentsofdirectoryatpath:nstemporarydirectory () Error:nil];      for inch tempdir) {        * filepath = [Nstemporarydirectory () stringbyappendingpathcomponent:file];        [Defaultmanager Removeitematpath:filepath Error:nil];    }

Where the five files should be stored

documents/
This directory holds user data. Store which files you want to expose to the user, and decide which user to create, import, edit, delete. For example: For a file editor app, the corresponding text file is stored in this directory. For a video app, the user's video file should be placed in this directory.

library/application support/
Store the data created by the app, but the data is transparent to the user. For example, coredata files can be stored here.

tmp
Files that are stored in temporary files that do not need to be used again the next time you log in. The app should be responsible for cleaning up the files when it's not needed, and the system will clean up the directory when the app is not running.

library/caches/
Store files that are longer than the temporary file life cycle time, but are shorter than the support file. Cache files are typically used to improve system performance. For example, the cache file for the database. Note that the system may delete this directory to free up disk space, so these files must be renewable. For example, the browser's cache file, even if deleted, still can be normal Web access.

Ios_sn_ Sandbox file operation and location

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.