Writing and reading of sandbox and simple objects (preview)

Source: Internet
Author: User

Sandbox: Every iOS app creates a file system directory for itself, a separate, closed, secure space called a sandbox

Sandbox is a security system

Features: 1. The range of activities for each application is limited to its own sandbox.

2. You can't go across your sandbox to access content in another application sandbox (IOS8 is already partially open)

3. Applications requesting or accepting data out-of-date require permission authentication

The application's sandbox directory will have three folders Documents, Library (caches and preferences directories below), TMP

Document: Save data that needs to be persisted when the app is run itunes automatically backs up the directory

get the Document directory

nsarray *path = nssearchpathfordirectoriesindomains(nsdocumentdirectory, Nsuserdomainmask, YES);

NSLog(@ "%@", path);

nsstring *documentdirectory = [path objectatindex:0];

NSLog(@ "%@", documentdirectory);

// get tmp directory: Save the temporary data required by the application to run, and then drop the corresponding files from the directory to delete, there is no runtime, the system may also clear the directory files, itunes will not sync the directory

nsstring *tmpdir = nstemporarydirectory();

NSLog(@ "%@", TmpDir);

Library: Default settings and other status information for stored programs

// get Library directory

nsarray *paths = nssearchpathfordirectoriesindomains(nslibrarydirectory, Nsuserdomainmask, YES);

nsstring *pathlib = [Paths objectatindex:0 ];

NSLog(@ "%@", pathlib);

// get library/caches directory: Store cache files

nsarray *path1 = nssearchpathfordirectoriesindomains(nscachesdirectory, Nsuserdomainmask, YES);

nsstring *pathcaches = [path1 objectatindex:0];

NSLog(@ "%@", pathcaches);

// get library/preferences directory: Save all your app's preferences

nsarray *path2 = nssearchpathfordirectoriesindomains(nslibrarydirectory, Nsuserdomainmask, YES);

nsstring *pathpre = [[path2 objectatindex:0] stringbyappendingpathcomponent :@ "Preferences"];

NSLog(@ "%@", Pathpre);

// Get the package path for the application

nsstring *imagepath = [nsbundle mainbundle]. ResourcePath;

NSLog(@ "%@", ImagePath);

// string object to write to file

// construct string Object

nsstring *strpath = [documentdirectory stringbyappendingstring:@ "/text.txt"];

// construct string Object

nsstring *foo_str = @ "This is a test";

// string storage

[Foo_str writetofile: strpath atomically:YES encoding:nsutf8stringencoding error:nil];

// read string object from File

nsstring *str = [nsstring stringwithcontentsoffile: strpath encoding: Nsutf8stringencoding error:nil];

NSLog(@ "%@", str);

// Array Object write file

// construct the storage path of the array object

nsstring *arraypath = [documentdirectory stringbyappendingstring:@ "/array.plist"];

// construct array Object

nsarray *foo_array = @[@ "Hjp", @ "hly", @ "LCL"];

// Store array Objects

[Foo_array writetofile: Arraypath atomically:YES];

nsarray *reultarray = [nsarray arraywithcontentsoffile: Arraypath];

NSLog(@ "%@", Reultarray);

Writing and reading of sandboxed and simple Objects (preview)

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.