IOS: Sandbox, preferences, archive, archive

Source: Internet
Author: User

one, sandbox and application packages

The iOS application can only read files in the file system created for the program and cannot be accessed anywhere else, this area is called a sandbox

iOS Common directory:–bundle–documents–library/caches–library/preference–tmp Show mac hidden file: Defaults write Com.apple.finder appleshowallfiles-bool true hide mac hidden file: Defaults write Com.apple.finder Appleshowallfiles-bool false

Application package Path: resource pool/developer/coresimulator/devices (device plist file)/data/containers/bundle/application (application package)


Sandbox path: Repository/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 files documents: Saves data that needs to be persisted when the app is run. The directory is backed up when itunes synchronizes the device. For example, a game app can save a game archive in this directory library/caches: Save data that needs to be persisted when the app runs, and itunes syncs the device without backing up the directory. Non-essential data with a large storage volume and no backup required library/preference: Save all preferences for your app, and the iOS Settings app will find the app's settings information in that directory. When itunes syncs the device, it backs up the directory tmp: Save the temporary data that is required to run the app, 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 device1, get the program's home directory

NSString *home = Nshomedirectory ();

Common ways to get a sandbox directory:

2. Get Documents Directory

Nsuserdomainmask representative from the user folder to find

YES represents the wavy character "~" in the expanded path

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

There is only one matching directory, so there is only one element in this collection

NSString *doc = documents[0];

Get the directory name using string concatenation

NSString *doc2 = [Home stringbyappendingpathcomponent:@ "Documents"];

Attention:

It is not recommended to use string concatenation to get the directory because the new version of the operating system may modify the default directory name.

3. Get the Cache directory

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

NSString *cache = caches[0];

4. Preferences Catalogue

Access to settings information in this directory through the Nsuserdefaults class

5. Get the TMP directory

NSString *tmpdir = Nstemporarydirectory ();

Description: Obtaining a path is only a necessary procedure to operate on a file under a path.

second, preferences (regardless of the location of the program, can be set to save the data to the local disk, as the global data to access, such as the interface can be used to pass data when jumping)Write Preferences:• Many iOS apps support preferences such as saving usernames, passwords, font size, and more, iOS offers a standard set of solutions to add preferences to your app • Each app has a nsuserdefaults instance to access preferences • For example: Save username, Font size, whether to log on automatically

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

[Defaults setobject:@ "itcast.cn" forkey:@ "UserName"];

[Defaults setfloat:18.0f forkey:@ "FontSize"];

[Defaults setbool:yes forkey:@ "Autologin"];

preferences for reading:

Read System Preferences

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

NSString *username = [Defaults objectforkey:@ "UserName"];

float fontSize = [Defaults floatforkey:@ "FontSize"];

BOOL autologin = [Defaults boolforkey:@ "Autologin"];

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];

third, object archiving, archiving (also used to store data)1, Nskeyedarchiver Archive, archive (only applicable to OC built-in objects)

+ (BOOL) Archiverootobject: (ID) rootobject tofile: (NSString *) path;//Archive

+ (ID) unarchiveobjectwithfile: (NSString *) path;//Archive

2, Nskeyedarchiver Archive, archive (custom objects, must be implemented <NSCoding> protocol)

-(void) Encodewithcoder: (Nscoder *) acoder;//Archived Protocol method

-(ID) Initwithcoder: (Nscoder *) adecoder;//protocol method for archiving

+ (BOOL) Archiverootobject: (ID) rootobject tofile: (NSString *) path;//Archive

+ (ID) unarchiveobjectwithfile: (NSString *) path;//Archive

3. Archive and Archive multiple objects

-(Instancetype) Initforwritingwithmutabledata: (Nsmutabledata *) data;//to create an archive object with a mutable data

-(void) Encodeobject: (ID) objv forkey: (NSString *) key;//archive objects

-(void) finishdecoding//complete archive (must be performed)

-(Instancetype) Initforreadingwithdata: (NSData *) data;//use a data to create a solution archive object, which is obtained from the file

-(ID) Decodeobjectforkey: (NSString *) key;//Solution Archive

IOS: Sandbox, preferences, archive, archive

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.