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. Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be kept in this directory, and itunes backup and recovery will include this directory library: default settings or other status information for 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. A: Get app sandbox root path:-(void) dirhome{nsstring *dirhome=nshomedirectory (); NSLog (@ "App_home:%@", dirhome); } B: Get the Documents directory path:-(NSString *) dirdoc{//[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentsdirectory = [Paths objectatindex:0]; NSLog (@ "App_home_doc:%@", documentsdirectory); return documentsdirectory; } C: Get library directory path:-(void) dirlib{//[nshomedirectory () stringbyappendingpathcomponent:@ "LIbrary "]; Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString *librarydirectory = [Paths objectatindex:0]; NSLog (@ "App_home_lib:%@", librarydirectory); } D: Get the cache directory path:-(void) dircache{Nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, NSUserDom Ainmask, YES); NSString *cachepath = [Cacpath objectatindex:0]; NSLog (@ "App_home_lib_cache:%@", CachePath); } e: Get tmp directory path:-(void) dirtmp{//[nshomedirectory () stringbyappendingpathcomponent:@ "TMP"]; NSString *tmpdirectory = Nstemporarydirectory (); NSLog (@ "app_home_tmp:%@", tmpdirectory); } F: 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"); } g: 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"); } h: Write data to 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"); } I: Read file data:-(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); } J: File attribute:-(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); }} k: 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 Management Nsfilemanager