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