IOS Sandbox path acquisition, creating files

Source: Internet
Author: User

There are four main folders under the sandbox: document,caches,tmp,library

Document PATH program run-time generated files, this file do not save larger files, such as audio, video class, because here things will be uploaded

Caches paths are typically used for file downloads, storage (not uploaded)

TMP temp file. Should be emptied after the program is finished

Sandbox folder path gets:

To get the sandbox path:

1 nsstring *sandboxpath = Nshomedirectory ();

Get Document:

1 nsstring *documentpath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) [ 0];

Get caches path:

1 nsstring *cacherpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [  0];

Get the TMP path:

1 nsstring *tmppath = Nstemporarydirectory ();

To create a file under a sandbox:

1. Get the path

2. stitching file name (including suffix)

3. Write the content to a file

Example: Write a TXT file:

1 //NSString Write2     3NSString *str =@"Two Fools";4     5     //Get document Path6NSString *docpath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) [0];7     8     //splicing the previous TXT file9NSString *filepath = [DocPath stringbyappendingpathcomponent:@"Coco.txt"];Ten      One     //Bar string Write to TXT file A[Str writetofile:filepath atomically:yes encoding:nsutf8stringencoding Error:nil];

Example: Write an array file:

1 //Nsarray2     3Nsarray *array = @[@"Two Fools",@"three fools .",@"Cui Hua",@"Yelianzen",@"Zhao Zhi Days",@"Wang Nima"];4     5NSString *tmppath1 =nstemporarydirectory ();6     7NSString *tmpfilepath = [tmpPath1 stringbyappendingpathcomponent:@"tmp.plist"];8     9[Array Writetofile:tmpfilepath atomically:yes];

Example: Write a dictionary file:

1Nsdictionary *dic = @{@"number 1th":@"XXXXX",@"Number 2nd":@"XXXXX",@"Number 3rd":@"XXXXX",@"Number 4th":@"XXXXX",@"Number 5th":@"XXXXX"};2     3NSString *dicpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];4NSString *dicfilepath = [Dicpath stringbyappendingpathcomponent:@"dic.plist"];5     6[DiC Writetofile:dicfilepath Atomically:yes];

Example: Write an image file:

1NSString *cacherpath2 = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];2     3NSString *imgfilepath = [CacherPath2 stringbyappendingpathcomponent:@"123.png"];4 5NSData *data = [[NSData alloc]initwithcontentsoffile:[[nsbundle Mainbundle] Pathforresource:@"1"OfType:@"PNG"]];6     7[Data Writetofile:imgfilepath Atomically:yes];

IOS Sandbox path acquisition, creating files

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.