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.
1. Each application is within its own sandbox (hint: already open in IOS8)
2. You can't go across your sandbox to access the contents of another application sandbox
3. Applications requesting or receiving data outside the application require permission authentication
Get home directory NSString *homepath = Nshomedirectory (); NSLog (@ "home%@", HomePath); Get documents Directory//way 1:nsarray *documents = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmas K, YES); NSString *documentspath = [documents Lastobject]; NSLog (@ "%@", Documentspath); Mode 2: Stitching string NSString *documentpath2 = [HomePath stringbyappendingpathcomponent:@ "Documents"]; NSLog (@ "documenpath2:%@", documentPath2); Get caches cache directory Nsarray *caches = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString *cachespath = [caches lastobject]; NSLog (@ "cachespath:%@", Cachespath); Get temp directory NSString *tmp = Nstemporarydirectory (); NSLog (@ "tmp:%@", TMP); Create a file in the documents directory Nsfilemanager *FM = [Nsfilemanager Defaultmanager]; NSString *filename = [Documentspath stringbyappendingpathcomponent:@ "1.txt"]; NSString *str = @ "Hello world"; NSData *data = [str datausingencoding:nsutf8stringencoding]; if ([FM createfileatpath:filename contents:data Attributes:nil]) {NSLog (@ "file create successful"); }//read out the contents of the file Nserror *error = nil; NSString *STR2 = [NSString stringwithcontentsoffile:filename encoding:nsutf8stringencoding error:&error]; if (!error) {NSLog (@ "str2:%@", str2); } else {NSLog (@ "%@", error); }//nsbundle objects: Pictures, Xib, StoryBoard, plist files are all placed in the application package nsbundle *bundle = [NSBundle mainbundle]; Gets the path of the file in the package; NSString *path = [bundle pathforresource:@ "Array" oftype:@ "plist"]; Load the Xib file in the package nsarray *views = [Bundle loadnibnamed:@ "Cell" owner:self Options:nil];
User Preferences Exercise:
Nsuserdefaults *userdeafults = [Nsuserdefaults standarduserdefaults]; [Userdeafults setobject:@ "admin" forkey:@ "userName"]; [Userdeafults setobject:[nsdate Date] forkey:@ "Logindate"]; [Userdeafults setinteger:2 forkey:@ "Logintimes"]; Write preferences to a file [userdeafults synchronize];//Sync method //Get home directory nsstring *homepath = Nshomedirectory (); NSLog (@ "%@", HomePath); Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; NSString *username = [userdefaults objectforkey:@ "UserName"]; NSDate *logindate = [userdefaults objectforkey:@ "Logindate"]; NSString *logintimes = [userdefaults objectforkey:@ "Logintimes"]; NSLog (@ "%@,%@", username,logintimes); NSDateFormatter *DF = [[NSDateFormatter alloc]init]; Df.dateformat = @ "Mm-dd hh-mm-ss"; NSLog (@ "logindate:%@", [DF Stringfromdate:logindate]);
Note: When setting the data, Userdefaults does not write immediately, but instead writes the cached data to the local disk according to the timestamp. So when the set method is called, the data may not have been written to the disk, and the application terminates. The above problem can be forced by calling the Synchornize method: [Defaults synchronize];
Three, the application of sandbox structure analysis
Package: (layer in) contains all the resource files and executable files
Documents: Save the data that needs to be persisted at run time, which is backed up when itunes synchronizes the device. For example, a game app can save a game archive in that directory
TMP: Save the temporary data that is required at run time, and then delete the corresponding file from the directory when you are finished. When the app is not running, the system may also purge files in that directory. itunes does not back up this directory when syncing the device
Library/caches: Save when the app runs? data that needs to be persisted, itunes does not back up the directory when syncing the device. Non-critical data with large storage volumes and no backup required
Library/preference: Save all of your app's preferences, iOS settings (settings) should be in this record to find the settings information. This directory is backed up when itunes syncs the device * Refer to other people's Please forgive me (all knowledge points, stole a lazy) *
An analysis of IOS sandbox mechanism