-(void) dirhome{ nsstring *dirhome=nshomedirectory (); NSLog (@ "App_home:%@", dirhome);}
the sandbox mechanism for iOS, apps can only access files in their own app directory. Unlike Android, IOS does not have the concept of an SD card 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.
Full path above: User--resource library->application Support->iphone simulator->6.1->applications
Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be kept in that directory, which is included in itunes backup and restore;
Library: The default settings or other status information of the stored program;
Library/caches: Store cache files, save the persisted data of the application, use to apply the upgrade or save the data after the app is closed, will 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 files.
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
How does iOS get the sandbox path and how do I manipulate files? Here's the answer.
Get the app sandbox root path:
-(void) dirhome{ nsstring *dirhome=nshomedirectory (); NSLog (@ "App_home:%@", dirhome);}
Get documentsdirectory path:
Get Documents Directory-(NSString *) dirdoc{ //[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentsdirectory = [Paths objectatindex:0]; NSLog (@ "App_home_doc:%@", documentsdirectory); return documentsdirectory;}
To get the Library directory path:
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:
Get the cache directory-(void) dircache{ nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString *cachepath = [Cacpath objectatindex:0]; NSLog (@ "App_home_lib_cache:%@", CachePath);}
GetTmpdirectory path:
Get TMP directory-(void) dirtmp{ //[nshomedirectory () stringbyappendingpathcomponent:@ "tmp"]; NSString *tmpdirectory = Nstemporarydirectory (); NSLog (@ "app_home_tmp:%@", tmpdirectory);}
To create a folder:
Create Folder-(void *) createdir{ nsstring *documentspath =[self Dirdoc]; Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"]; Create 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
Create 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:
Write 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:
Read 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:
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:
Delete file-(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");}
iOS file system: https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/ Filesystemprogrammingguide/filesystemoverview/filesystemoverview.html#//apple_ref/doc/uid/tp40010672-ch2-sw2
IOS file Operations