IOS Sandbox structure usage examples

Source: Internet
Author: User

Statement: The document is through their own search online information and after years of experience and summed up, I hope to have some help, what is not the appropriate expenditure also ask you more guidance!

The sandbox mechanism (sandbox) in iOS is a security system that specifies that an application can only read files within a folder created for the app and cannot access content elsewhere. All non-code files are saved in this place, such as slices, sounds, attribute lists, and text files.

The structure of the Sandbox (sandbox):

1, Myapp.app

2. Documents

3. Library

4. tmp

The role of each directory and whether it will be synced by itunes :

1, Myapp.app

Storage content: The directory contains the data for the application itself, including the resource file and the executable file. The entire directory is read-only to prevent tampering, and the directory is signed when the app is installed.
will be synced by itunes: No

2.0. Documents

Store Content: storing non-renewable data files (storing user data or other information that should be backed up regularly)
will be synced by itunes: Yes

2.1, Documents/inbox

Store data: used to save files opened by external apps requesting the current application. For example: an existing application book.app can open a TXT file, while in another application BookShop.app there is a a.txt file that needs to be book.app opened, but the sandbox mechanism does not allow book direct access BookShop to the files in the sandbox, so Apple's solution is to speak BookShop Copy the A.txt file in to the book Documents/Inbox bottom, and then let the book a.txt open.
will be synced by itunes: Yes

3.0. Library

Store Content: It is recommended to store default data or other status information.
will be synced with itunes: Yes (not including caches subdirectories)

3.1, Library/caches

Storage content: The main storage cache files, the use of the process of cache files can be stored here. Used to save a renewable file. such as network requests, but the general application is responsible for cleaning up the data.
will be synced by itunes: No

3.2, Library/preferences

Store Content: store the app's preferences file, generally we use nsuserdefaules write data will exist in the plist file of this file.

will be synced by itunes: Yes

4. tmp

Storage: A variety of temporary files, to ensure that you start the unnecessary files, when the application no longer need these files should be actively deleted, because the system can be cleaned at any time.
will be synced by itunes: No

To obtain the methods for each directory:

    Get root directoryNSString *homepath =Nshomedirectory ();NSLog (@ "\ n root directory:%@", HomePath);Get Documents DirectoryNSString *docmentpath = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory,Nsuserdomainmask,YES) Firstobject];NSLog (@ "\ n Get documents directory:%@", Docmentpath);A different wayNsurl *documenturl = [[[Nsfilemanager Defaultmanager] Urlsfordirectory:NSDocumentDirectory Indomains:Nsuserdomainmask] Firstobject];NSLog (@ "\ n Get documents directory%@", Documenturl);Get LibrarysNSString *librarypath = [Nssearchpathfordirectoriesindomains (Nslibrarydirectory,Nsuserdomainmask,YES) Firstobject];NSLog (@ "\ n Get libray directory:%@", LibraryPath);Also another wayNsurl *libraryurl = [[[Nsfilemanager Defaultmanager] Urlsfordirectory:Nslibrarydirectory Indomains:Nsuserdomainmask] Firstobject];NSLog (@ "\ n Get libray directory:%@", Libraryurl);Get the Cache directoryNSString *cachepath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, YES) firstobject]; NSLog (@ "\ n get the cache directory:%@", CachePath); Nsurl *cacheurl = [[[Nsfilemanager Defaultmanager] urlsfordirectory:nscachesdirectory indomains:  Nsuserdomainmask]firstobject]; NSLog (@ "\ n get the cache directory:%@", Cacheurl); //temp directory nsstring *temppath = nstemporarydirectory (); NSLog (@ "Temp directory:%@", TempPath);               
Instance:

Get Documents Path

-(NSString *) Getdocumentspath

{

Get Documents Path

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString *path = [Paths objectatindex:0];

NSLog (@ "path:%@", Path);

return path;

}

Create a folder

-(void) createdirectory{

NSString *documentspath =[self Getdocumentspath];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

NSString *iosdirectory = [Documentspath stringbyappendingpathcomponent:@ "IOS"];

BOOL issuccess = [FileManager createdirectoryatpath:iosdirectory withintermediatedirectories:yes attributes:nil Error : nil];

if (issuccess) {

NSLog (@ "Success");

} else {

NSLog (@ "fail");

}

}

Create a file

-(void) createfile{

NSString *documentspath =[self Getdocumentspath];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

NSString *iospath = [Documentspath stringbyappendingpathcomponent:@ "IOS.txt"];

BOOL issuccess = [FileManager createfileatpath:iospath contents:nil Attributes:nil];

if (issuccess) {

NSLog (@ "Success");

} else {

NSLog (@ "fail");

}

}

Write data to a file

-(void) writefile{

NSString *documentspath =[self Getdocumentspath];

NSString *iospath = [Documentspath stringbyappendingpathcomponent:@ "IOS.txt"];

NSString *content = @ "I want to write data";

BOOL issuccess = [content Writetofile:iospath atomically:yes encoding:nsutf8stringencoding Error:nil];

if (issuccess) {

NSLog (@ "write success");

} else {

NSLog (@ "write fail");

}

}

Reading data

-(void) readfilecontent{

NSString *documentspath =[self Getdocumentspath];

NSString *iospath = [Documentspath stringbyappendingpathcomponent:@ "IOS.txt"];

NSString *content = [NSString stringwithcontentsoffile:iospath encoding:nsutf8stringencoding Error:nil];

NSLog (@ "read success:%@", content);

}

Determine if a file exists

-(BOOL) Issxistatpath: (NSString *) filepath{

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

BOOL isexist = [FileManager Fileexistsatpath:filepath];

return isexist;

}

Calculate the size of a file

-(unsigned long Long) Filesizeatpath: (NSString *) filepath{

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

BOOL isexist = [FileManager Fileexistsatpath:filepath];

if (isexist) {

unsigned long long fileSize = [[FileManager attributesofitematpath:filepath Error:nil] fileSize];

return fileSize;

} else {

NSLog (@ "file is not exist");

return 0;

}

}

Calculate the size of the entire file

-(unsigned long Long) Foldersizeatpath: (nsstring*) folderpath{

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

BOOL isexist = [FileManager Fileexistsatpath:folderpath];

if (isexist) {

Nsenumerator *childfileenumerator = [[FileManager Subpathsatpath:folderpath] objectenumerator];

unsigned long long foldersize = 0;

NSString *filename = @ "";

while ((FileName = [Childfileenumerator nextobject]) = nil) {

nsstring* Fileabsolutepath = [FolderPath stringbyappendingpathcomponent:filename];

Foldersize + = [self filesizeatpath:fileabsolutepath];

}

Return foldersize/(1024.0 * 1024.0);

} else {

NSLog (@ "file is not exist");

return 0;

}

}

deleting files

-(void) deletefile{

NSString *documentspath =[self Getdocumentspath];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

NSString *iospath = [Documentspath stringbyappendingpathcomponent:@ "IOS.txt"];

BOOL issuccess = [FileManager removeitematpath:iospath error:nil];

if (issuccess) {

NSLog (@ "delete success");

}else{

NSLog (@ "delete fail");

}

}

-(void) movefilename

{

NSString *documentspath =[self Getdocumentspath];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

NSString *filepath = [Documentspath stringbyappendingpathcomponent:@ "IOS.txt"];

NSString *movetopath = [Documentspath stringbyappendingpathcomponent:@ "IOS.txt"];

BOOL issuccess = [FileManager moveitematpath:filepath topath:movetopath Error:nil];

if (issuccess) {

NSLog (@ "rename success");

}else{

NSLog (@ "rename fail");

}

}

File Rename

-(void) renamefilename

{

Rename a file by moving the file

NSString *documentspath =[self Getdocumentspath];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

NSString *filepath = [Documentspath stringbyappendingpathcomponent:@ "IOS.txt"];

NSString *movetopath = [Documentspath stringbyappendingpathcomponent:@ "Rename.txt"];

BOOL issuccess = [FileManager moveitematpath:filepath topath:movetopath Error:nil];

if (issuccess) {

NSLog (@ "rename success");

}else{

NSLog (@ "rename fail");

}

}

 

IOS Sandbox structure usage examples

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.