http://blog.csdn.net/xyz_lmn/article/details/8968213
The sandbox mechanism for iOS, apps can only access files in their own app directory. Unlike Android, iOS does not have an SD card concept and does not have direct access to images, videos, and other content. Content generated by iOS apps, like, files, cached content, and so on, must be stored in their own sandbox. By default, each sandbox contains 3 folders: Documents, Library, and TMP. The library contains caches, preferences directories.
The full path above is: User--resource library->application Support->iphone simulator->6.1->aplications
Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be saved in this directory, whichis included in itunes backup and restore
Library: The default settings or other status information of the stored program;
Library/caches: Store the cache file, save the persisted data of the application, use it to apply the upgrade or save the data after the app is closed, and not be synced by itunes, so in order to reduce the time of synchronization, consider putting some larger files into this directory without needing backup.
TMP: Provides an immediate place to create temporary files, but does not need to be persisted, after the application is closed, the data in the directory will be deleted, or the system may be cleared when the program is not running.
APP Sandbox
How does iOS get the sandbox path and how do I manipulate files? Here's the answer.
Get the app sandbox root path:
[CPP]View PlainCopyprint?
- -(void) dirhome{
- NSString *dirhome=nshomedirectory ();
- NSLog (@"App_home:%@", dirhome);
- }
Get documentsdirectory path:
[CPP]View PlainCopyprint?
- //get documents directory
- -(nsstring *) dirdoc{
- //[ Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];  
- nsarray *paths = nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, yes);
- nsstring * documentsdirectory = [paths objectatindex:0];
- nslog (@
- return documentsdirectory;
- }
To get the Library directory path:
[CPP]View PlainCopyprint?
- Get Library Directory
- -(void) dirlib{
- //[nshomedirectory () stringbyappendingpathcomponent:@ "Library";
- Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);
- NSString *librarydirectory = [Paths objectatindex:0];
- NSLog (@"App_home_lib:%@", librarydirectory);
- }
get the Cache directory path:
[CPP]View PlainCopyprint?
- Get the Cache directory
- -(void) dircache{
- Nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
- NSString *cachepath = [Cacpath objectatindex:0];
- NSLog (@"App_home_lib_cache:%@", CachePath);
- }
get the TMP directory path:
[CPP]View PlainCopyprint?
- Get the TMP directory
- -(void) dirtmp{
- //[nshomedirectory () stringbyappendingpathcomponent:@ "TMP"];
- NSString *tmpdirectory = Nstemporarydirectory ();
- NSLog (@"app_home_tmp:%@", tmpdirectory);
- }
To create a folder:
[CPP]View PlainCopyprint?
- Create a folder
- -(void *) createdir{
- NSString *documentspath =[self Dirdoc];
- Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
- NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
- //Create a directory
- BOOL res=[filemanager createdirectoryatpath:testdirectory withintermediatedirectories:yes attributes:nil Error: NIL];
- if (res) {
- NSLog (@"folder creation succeeded");
- }Else
- NSLog (@"folder creation failed");
- }
Create a file
[CPP]View PlainCopyprint?
- Create a file
- -(void *) createfile{
- NSString *documentspath =[self Dirdoc];
- NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
- Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
- NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
- BOOL res=[filemanager createfileatpath:testpath contents:nil Attributes:nil];
- if (res) {
- NSLog (@"file creation succeeded:%@", Testpath);
- }Else
- NSLog (@"file creation failed");
- }
Write data to file:
[CPP]View PlainCopyprint?
- Write a file
- -(void) writefile{
- NSString *documentspath =[self Dirdoc];
- NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
- NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
- NSString *[email protected]"Test write content! ";
- BOOL res=[content writetofile:testpath atomically:yes encoding:nsutf8stringencoding Error:nil];
- if (res) {
- NSLog (@"file write succeeded");
- }Else
- NSLog (@"file write Failed");
- }
Read file data:
[CPP]View PlainCopyprint?
- Read the file
- -(void) readfile{
- NSString *documentspath =[self Dirdoc];
- NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
- NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
- NSData *data = [NSData Datawithcontentsoffile:testpath];
- NSLog (@ "file read succeeded:%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]);
- NSString *content=[nsstring Stringwithcontentsoffile:testpath encoding:nsutf8stringencoding Error:nil];
- NSLog (@"file read succeeded:%@", content);
- }
File properties:
[CPP]View PlainCopyprint?
- File properties
- -(void) fileattriutes{
- NSString *documentspath =[self Dirdoc];
- NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
- Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
- NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
- Nsdictionary *fileattributes = [FileManager attributesofitematpath:testpath error:nil];
- Nsarray *keys;
- ID key, value;
- Keys = [FileAttributes AllKeys];
- int count = [keys count];
- For (int i = 0; i < count; i++)
- {
- key = [Keys objectatindex:i];
- Value = [FileAttributes Objectforkey:key];
- NSLog (@"key:%@ for Value:%@", key, value);
- }
- }
To delete a file:
[CPP]View PlainCopyprint?
- deleting files
- -(void) deletefile{
- NSString *documentspath =[self Dirdoc];
- NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
- Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
- NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
- BOOL Res=[filemanager Removeitematpath:testpath Error:nil];
- if (res) {
- NSLog (@"file deletion succeeded");
- }Else
- NSLog (@"file deletion failed");
- NSLog (@"file exists:%@", [FileManager isexecutablefileatpath:testpath][email protected]"yes": @"NO");
- }
/*** @author Zhang Xingye * http://blog.csdn.net/xyz_lmn* iOS starter Group: 83702688* Android Development Advanced Group: 241395671* my Sina Weibo:
@ Zhang Xingye tbow* My e-mail: xy-zhang#163.com (#->@) */https://developer.apple.com/library/ios/#documentation/filemanagement/ Conceptual/filesystemprogrammingguide/filesystemoverview/filesystemoverview.html#//apple_ref/doc/uid/ Tp40010672-ch2-sw2
iOS Learning Notes (17)--File operations (Nsfilemanager)