iOS sandbox mechanism
iOS applications can only read files in the file system created for the change, not anywhere else, this area is sandboxed, so all of the non code files are saved here, such as images, icons, sounds, images, attribute lists, text files, and so on.
- Each application has its own storage space
- Applications cannot go over their walls to access the contents of other storage space
Open Emulator Sandbox Directory
Method 1, you can set the display hidden file, and then open it directly under the Finder. Set the method for viewing hidden files as follows: Open the terminal, enter the name
<p class= "P1" > Show mac hidden files command:
Copy Code code as follows:
Defaults write Com.apple.finder appleshowallfiles-bool true</p><p class= "P1" >
To hide a mac hidden file command:
Copy Code code as follows:
Defaults write Com.apple.finder Appleshowallfiles-bool false</p>
You can now see the repository folder.
Locate the/application Support/iphone simulator/folder after you open the resource pool. This is the sandbox directory for each program in the simulator.
Method 2, this method is more convenient, on the Finder point-> to then hold down the "option" key, will appear "resource pool", the other ibid
Directory structure
By default, each sandbox contains 3 folders: Documents, Library, and TMP. Application can only read and write files in several directories because of the sandbox mechanism applied
Documents: Apple recommends that the file data that is established in the program or browsed to the program be saved in the directory, which will be included in the itunes backup and restore
Library: Stores the default settings or other state information of the program;
Library/caches: Store cached files, itunes does not back up this directory, the files in this directory will not be deleted in the application exit
TMP: Provides a place to create temporary files immediately.
itunes backs up all documents and library files when it synchronizes with the iphone.
When the iphone is restarted, all TMP files are discarded.
This is the three directories mentioned above: Documents, Library, tmp
A few common code examples:
1, get the program's home directory
Copy Code code as follows:
NSString *homedirectory = Nshomedirectory ();
NSLog (@ "path:%@", homedirectory);
2. Get Document Catalogue
Copy Code code as follows:
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *path = [Paths objectatindex:0];
NSLog (@ "path:%@", Path);
3. Get Cache Directory
Copy Code code as follows:
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
NSString *path = [Paths objectatindex:0];
NSLog (@ "%@", Path);
4. Get Library catalogue
Copy Code code as follows:
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);
NSString *path = [Paths objectatindex:0];
NSLog (@ "%@", Path);
5. Get TMP Directory
Copy Code code as follows:
NSString *tmpdir = Nstemporarydirectory ();
NSLog (@ "%@", tmpdir);
6. Write to File
Copy Code code as follows:
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *docdir = [Paths objectatindex:0];
if (!docdir) {
NSLog (@ "The Documents directory is not found");
}
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "Contents", @ "content", nil];
NSString *filepath = [Docdir stringbyappendingpathcomponent:@ "TestFile.txt"];
[Array Writetofile:filepath atomically:yes];
7. Write to File
Copy Code code as follows:
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *docdir = [Paths objectatindex:0];
NSString *filepath = [Docdir stringbyappendingpathcomponent:@ "TestFile.txt"];
Nsarray *array = [[Nsarray Alloc]initwithcontentsoffile:filepath];
NSLog (@ "%@", array);
8, to determine whether a file exists, incoming full path (Fileexistsatpath)
Copy Code code as follows:
Creating a File Manager
Nsfilemanager * FileManager = [Nsfilemanager Defaultmanager];
NSString * documents = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
NSString * FilePath = [documents stringbyappendingpathcomponent:@ "test"];
To determine if a file exists, pass in the full path
if ([FileManager Fileexistsatpath:filepath]) {
NSLog (@ "It is exit");
}
9. Create a catalogue in documents
Copy Code code as follows:
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentsdirectory = [Paths objectatindex:0];
NSLog (@ "documentsdirectory%@", documentsdirectory);
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
NSString *testdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "test"];
Create a table of contents
[FileManager createdirectoryatpath:testdirectory withintermediatedirectories:yes Attributes:nil Error:nil];
10, create files in the directory
Copy Code code as follows:
NSString *testpath = [testdirectory stringbyappendingpathcomponent:@ "Test00.txt"];
NSString *testpath2 = [testdirectory stringbyappendingpathcomponent:@ "Test22.txt"];
NSString *testpath3 = [testdirectory stringbyappendingpathcomponent:@ "Test33.txt"];
NSString *string = @ "Write to content, write string";
[FileManager createfileatpath:testpath contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];
[FileManager createfileatpath:testpath2 contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];
[FileManager createfileatpath:testpath3 contents:[string datausingencoding:nsutf8stringencoding] attributes:nil];
11, get all the file names in the directory column
Two ways to get: Subpathsofdirectoryatpath and Subpathsatpath
Copy Code code as follows:
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentsdirectory = [Paths objectatindex:0];
NSLog (@ "documentsdirectory%@", documentsdirectory);
Nsfilemanager *filemanage = [Nsfilemanager Defaultmanager];
NSString *mydirectory = [documentsdirectory stringbyappendingpathcomponent:@ "test"];
Nsarray *file = [Filemanage subpathsofdirectoryatpath:mydirectory error:nil];
NSLog (@ "%@", file);
Nsarray *files = [Filemanage subpathsatpath:mydirectory];
NSLog (@ "%@", files);
12, FileManager Use the current directory of operations
Copy Code code as follows:
Creating a File Manager
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentsdirectory = [Paths objectatindex:0];
Change to the directory to be operated
[FileManager Changecurrentdirectorypath:[documentsdirectory Stringbyexpandingtildeinpath]];
Create file FileName File name, contents file contents, if Start no content can be set to Nil,attributes file properties, initially nil
NSString * FileName = @ "TestFileNSFileManager.txt";
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "Hello World", @ "Hello world1", @ "Hello world2", nil];
[FileManager createfileatpath:filename Contents:array Attributes:nil];
13, delete the file
Copy Code code as follows:
[FileManager Removeitematpath:filename Error:nil];