IOS: Sandbox's understanding and catalog fetching

Source: Internet
Author: User

The Sandbox is detailed:

•iOS apps can only read files in the file system created for the program, not anywhere else, this area is called a sandbox

iOS Common directory:–bundle//files in this directory are used to store application packages that contain applications and assets–documents//files in this directory are typically used for persistent storage of data and for backup, such as archive files–library/caches//files in this directory are typically used for persistent storage of data and no data is backed up–library/preference//files in this directory are used for persistent storage system device information and to back up data. –tmp //A temporary folder, used to store temporary files, once the program stops running, the contents of the saved file will disappear . Application Package Path: resource pool/developer/coresimulator/devices (device plist file)/data/containers/bundle/application (application package)
Sandbox path: Resource library/developer/coresimulator/devices (device plist file)/data/containers/data/application/(document, library, TMP ) library/(caches cache, Preferences user preferences)• Application Package: (bundle) contains all the resource files and executable filesDocuments: Saves data that needs to be persisted when the app runs, and when itunes synchronizes the device, it backs up the directory. For example, a game app can save a game archive in that directory•library/caches: Saves data that needs to be persisted when the app runs, and itunes syncs the device without backing up the directory. Non-critical data with large storage volumes and no backup required•library/preference: Save all your app's preferences, and the iOS Settings app will find the app's settings in that directory. This directory is backed up when itunes syncs the device•tmp: Save the temporary data required for the app to run, and then delete the corresponding file from the directory when you are finished using it. 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 deviceGet the Sandbox directory:1. Get home directory:

NSString *homepath = Nshomedirectory ();

2. There are two ways to obtain the documents directory:

Method One: Obtained by means of a function (Domains: Indicates a search domain, which is the storage area of the phone)

Nsarray *directorys = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString *documentspath = [Directorys lastobject];

Mode two: obtained by stitching the string (string concatenation is not recommended because the new version of the operating system may modify the default directory name)

NSString *documentspath2 = [HomePath stringbyappendingpathcomponent:@ "Documents"];

3. Get the caches cache directory (which can also be obtained by stitching strings, not listed here)

Nsarray *caches = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);

NSString *cachespath = [caches lastobject];

4. Get a temp directory from a function

NSString *tmp = Nstemporarydirectory ();

5.NSBundle objects: Pictures, Xib, storyboard, plist files are all placed in the application package.

NSBundle *bundle = [NSBundle mainbundle];

Get the path to a file in a package

NSString *path= [Bundle pathforresource:@ "Array" oftype:@ "plist"];

Load Xib file in package

Nsarray *views = [Bundle loadnibnamed:@ "Cell" owner:self Options:nil];

  

Tip: Getting a directory is not a destination, get a directory in order to manipulate files in that directory, such as creating files and storing data

6. Create a file 

Create a file Management object

Nsfilemanager *FM = [Nsfilemanager Defaultmanager];

Create a file

const char *PT = "How is You";

NSString *filename = [Documentspath stringbyappendingpathcomponent:@ "1.txt"];

NSData *data = [NSData datawithbytes:pt Length:strlen (PT)];

NSString *str = @ "How is You";

NSData *data = [str datausingencoding:nsutf8stringencoding];

if ([FM createfileatpath:filename contents:data Attributes:nil])

{

NSLog (@ "file creation succeeded");

}

7. Reading files

Direct Read

Nserror *error = nil;

Nsstringencoding enc = 4/*nsutf8stringencoding*/;

NSString *STR2 = [NSString stringwithcontentsoffile:filename usedencoding:&enc error:&error];

if (!error)

{

NSLog (@ "str2:%@", str2);

}

Else

{

NSLog (@ "read failed,%@", error);

}

IOS: Sandbox's understanding and catalog fetching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.