One, sand box
Each iOS application has its own directory to hold the data, which is called the Sandbox directory. The sandbox directory is a data security policy that is designed to allow only its own applications to access the directory and not allow other applications to access it, so that data is secured and data is not shared between applications.
Some unique applications, such as communication records, require specific APIs to share data.
The following is a brief introduction to the application's sandbox directory, the first intuitive look at the sandbox directory structure of the demo program.
The sandbox path for the application is:
/users/"User name"/library/developer/coresimulator/devices/8dfe7883-956e-41e3-b099-18c92609e027/data/containers/data /application/e82b708d-b06c-4837-ba59-74ee22ca7be4
As we can see, the sandbox directory has three subdirectories, respectively, documents,library,tmp
1.Documents
This directory is used to store very large files or data that needs to be updated very frequently and can be backed up in itunes or icloud. The directory is an array of only one element, so the code to get the directory location is as follows:
NSString *documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) lastObject ];
2. Library
The directory below has preferences and caches two subdirectories,preferences used to store the application settings data, to be able to make itunes or icloud backup; Caches is primarily used to store the app's cache files, which itunes does not back up.
The code to get the library directory location is as follows:
NSString *librarypath = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) lastObject];
the code to get the Preferences directory location is as follows:
NSString *preferencepath = [Nssearchpathfordirectoriesindomains (nspreferencepanesdirectory, NSUserDomainMask, YES) Lastobject];
the code to get the caches directory location is as follows:
NSString *cachepath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject];
3. tmp
This directory is used to store temporary files, and it is not able to make itunes or icloud backups. The user can access it and get the code for that directory as follows:
NSString *tmppath = Nstemporarydirectory ();
Second, the file operation
1. Create a folder
We create the test folder under the documents directory
NSString *documentpath =[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSLog (@"Document folder path is%@", documentpath); Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; NSString*testpath = [Documentpath stringbyappendingpathcomponent:@"Test"]; BOOL issuccess=[FileManager createdirectoryatpath:testpath withintermediatedirectories:yes Attributes:nil Error:nil]; NSLog (@"Did you create the folder successfully:%@", issuccess?@"Yes":@"No");
Created successfully
2. Create a file
Then the above code, we create the file under the test folder Test.txt
NSString *txtpath = [Testpath stringbyappendingpathcomponent:@ "test.txt"]; = [FileManager createfileatpath:txtpath contents:nil Attributes:nil]; NSLog (@ " successfully created the file:%@", istxtsuccess? @" Yes ":@"no");
Created successfully
You can see that the test.txt is still empty and we write the data below.
3. Write data to File
@" Hello world?? " ; = [teststring writetofile:txtpath atomically:yes encoding:nsutf8stringencoding Error:nil]; NSLog (@ " successfully written data:%@", iswritesuccess? @" Yes ":@"no");
Write success
4. Read the file
Below we read the contents of the Test.txt file we just created
NSString *readstring = [NSString stringwithcontentsoffile:txtpath encoding:nsutf8stringencoding Error:nil]; NSLog (@ " read the file content is%@", readString);
Successfully read
5. Delete Files
BOOL isdeletesuccess = [FileManager removeitematpath:txtpath error:nil]; NSLog (@ " successfully deleted files:%@", isdeletesuccess? @" Yes ":@"no");
Delete Succeeded
6, calculate the total size of the file under the path
+(float) Sizewithfilepath: (NSString *) path{Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; BOOL isdirectory=NO; /*The following method returns two parameters Isexist: whether the path exists isdirectory: If the path is a folder*/BOOL isexist= [FileManager Fileexistsatpath:path isdirectory:&Isdirectory]; if(!isexist) { return 0; } //if it is a folder, walk out of the file if(isdirectory) {//This method can get all the sub-paths (direct/indirect subpath) below this folderNsarray *subpaths =[FileManager Subpathsatpath:path]; floatTotalSize =0; for(NSString *subpathinchsubpaths) {NSString*fullsubpath =[path Stringbyappendingpathcomponent:subpath]; //determine if the sub-path is a folderBOOL Isdir =NO; [FileManager fileexistsatpath:fullsubpath isdirectory:&Isdir]; //The size of the file is calculated under the sub-path if(!isdir) {Nsdictionary*attributes =[FileManager Attributesofitematpath:fullsubpath Error:nil]; //The resulting result is in B, divided by 1024 * 1024 into MBTotalSize + =[attributes[nsfilesize] floatvalue]; }} nsstring*sizestring = [NSString stringWithFormat:@"%.2FMB", TotalSize/(1024x768*1024.0)]; NSLog (@"path%@ file size is%@", path,sizestring); returnTotalSize/(1024x768*1024.0); } Else{nsdictionary*attributes =[FileManager Attributesofitematpath:path Error:nil]; NSLog (@"path%@ file size is%f", Path,[attributes[nsfilesize] floatvalue]/(1024x768*1024.0)); return[Attributes[nsfilesize] floatvalue]/(1024x768*1024.0); }}
7. Delete all files under the path
+(void) Cleanfilewithfilepath: (NSString *) path{//NSLog (@ "Erase before file size is%.2fmb", [Fzyfilemanager Sizewithfilepath:path]);Nsfilemanager *filemanager =[Nsfilemanager Defaultmanager]; dispatch_queue_t Globalqueue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); Dispatch_async (Globalqueue,^{Nsarray*subpaths =[FileManager Subpathsatpath:path]; for(NSString *subpathinchsubpaths) {Nserror*error; NSString*fullpath =[path Stringbyappendingpathcomponent:subpath]; if([FileManager Fileexistsatpath:fullpath]) {[FileManager Removeitematpath:fullpath error:
&ERROR]; } } //NSLog (@ "File size is%.2FMB after purge", [Fzyfilemanager Sizewithfilepath:path]);Dispatch_sync (Dispatch_get_main_queue (), ^{//[Fzyfilemanager cleansuccess]; }); });}
Click here to download the relevant code
Sandbox directory and file operations for iOS