iOS Learning Note 22: Files created by the running iOS app

Source: Internet
Author: User
Tags home screen

IOS5 A more important feature of icloud, but there is also a problem, many apps have a lot of data in the documents under the app ( /Documents ) folder inside, so that Apple will reject off your app, Unless you keep the app in AppStore until you're done updating, it's obviously not possible, because you'll sync your files to icloud.

There is a way to not let Apple reject your app, one is to put these files exist Caches ( /Library/Caches ) folder inside, but this Apple said, if the low storage space will be cleaned up, so you may be cleared of this data, will lead to some column problems, If you don't care if these files are deleted, or if the data is stored for a few days, then that's it.

If you want the file not deleted but also do not back up, you can use the following code to mark, do not do backup processing

1-(BOOL) Addskipbackupattributetoitematurl: (Nsurl *) URL2 {3 assert ([[Nsfilemanager Defaultmanager] Fileexistsatpath: [URL Path]]);4Nserror *error =Nil;5BOOL success =[URL setresourcevalue: [NSNumber Numberwithbool:yes]6Forkey:nsurlisexcludedfrombackupkey Error: &ERROR];7     if(!success) {8NSLog (@"Error excluding%@ from backup%@", [URL lastpathcomponent], error);9     }Ten     returnsuccess; One }

1 #import<sys/xattr.h>2-(BOOL) Addskipbackupattributetoitematurl: (nsstring*) path3 {    4    if(System_version_less_than (@"5.1"))5     {6constchar* FolderPath =[pathfilesystemrepresentation];7       Const Char* Attrname ="Com.apple.MobileBackup";8u_int8_t AttrValue =1;9Intresult = Setxattr (FolderPath, Attrname,&attrvalue,sizeof(AttrValue),0,0);Ten        returnresult = =0; One    }  A    Else -     { - #ifndef __iphone_5_1 the     #defineNsurlisexcludedfrombackupkey @ "Nsurlisexcludedfrombackupkey" -     #endif -Nserror *error =Nil; -nsurl* URL =[Nsurlfileurlwithpath:path]; +Boolsuccess =[url Setresourcevalue:[nsnumbernumberwithbool:yes] - Forkey:nsurlisexcludedfrombackupkey +Error: &ERROR]; A        returnsuccess; at     } -}

iOS apps often create files at run time, but how do they store them? Are there any requirements?

Given the limited space available for mobile phones and the fact that Apple has icloud in mind, we do want to separate several categories of created files by their role, and set the file backup Properties attribute (iOS5.0.1 and above), so that the iOS system can better manage and treat the files we create.

File classification, storage path, and file attributes for iOS

IOS Data Storage guidelines and "do not back to up" file properties

IOS5 with icloud, Apple has updated its data storage guidelines to accommodate icloud storage, while adding "do not back up" file attributes to specify that files are not backed up and uploaded to icloud

Excerpts are as follows, for your reference:

IOS Data Storage Guidelines

icloud includes a backup that automatically backs up users ' iOS devices every day via Wi-Fi. Everything in the app's home directory will be backed up, in addition to the app bundle itself, the cache directory, and the temp directory. Purchased music, apps, books, Cameraroll, device settings, home screen, app organization, messages, ringtones will also be backed up. Because backups are done wirelessly and stored in icloud for each user, the app needs to minimize the amount of data it stores. Large files can prolong backup times and consume the user's available icloud space.

To ensure that backups are as fast and efficient as possible, applying stored data requires the following guidelines:

1. Only those user-generated documents or other data, or data that the app cannot recreate, should be stored in the/documents directory and automatically backed up to icloud.

2. Data that can be re-downloaded or generated should be stored in the/library/caches directory. For example, database cache files, downloadable files (magazines, newspapers, data used by map applications), etc. are in this category.

3. Temporary use of data should be stored in the/tmp directory. Although these files will not be backed up by icloud, the app will need to remember to delete the files after use so that it does not continue to occupy the user's device space.

4. Use the Do not backup property to specify files that need to remain on the device, even if there is low storage space. This property is required for those files that can be regenerated, but remain in low storage space, have an impact on the app's uptime, or if the user wants files to be available offline. This property can be used regardless of the file (including the documents directory) in the directory. These files are not deleted and are not included in the user's icloud or itunes backup. Since these files always occupy the storage space of the user's device, it is the responsibility of the application to periodically monitor and delete these files.

IOS 5.0.1 begins to support the "Do not backup" file properties, allowing developers to explicitly specify which files should be backed up, which local caches can be automatically deleted, and which files need not be backed up but not deleted. In addition, setting this property on the directory prevents all content in the directory and directory from being backed up.

Note that the do not back up property is only available for later versions of iOS5.0.1. Previous versions of the app need to store data to the/library/caches directory to avoid being backed up. Since the old system ignores this attribute, you need to make sure that the app is in all iOS versions and follows the Iosdata Storage guidelines above.

Key data

Content: User-created data files that cannot be recreated automatically after deletion and

Path: Home directory/documents

Properties: Do not set "do not back up"

Management: iOS systems are not purged when they are running out of storage space and are backed up to itunes or icloud

Cache data

Content: can be used for offline environment, can be repeated download duplicate generation, immediately when offline, the application itself can function

Path: Home directory/library/caches

Properties: Default

Management: Empty in case of insufficient storage space, and will not be automatically backed up to itunes and icloud

Read the cache directory

Nsarray *paths = nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES);  NSString *path = [paths Objectatindex:0];  NSLog (@ "%@      
Temporary data

Content: Files that are temporarily generated for an internal operation when the app is run

Path: Home Directory/tmp

Properties: Default

Management: May be purged by the iOS system at any time, and will not be automatically backed up to itunes and icloud, although ICloud does not back up these files, but after the app has finished using the data, be careful to remove it at any time to avoid occupying the user's device space

NSString *tmpdir = nstemporarydirectory ();  NSLog (@ "%@"
Offline data

Content: Similar to cached data, it can be re-downloaded and rebuilt, but users tend to expect data to remain in place while offline

Directory: Home directory/documents or home directory/library/custom Folder

Properties: Do not need to set in documents, put in the custom folder should be set "do not Backup"

Management: Similar to critical data, it will not be clear when there is not enough storage space, the application should clean up files that are no longer used, so as not to waste user device space

The project comes with a bundle of. Bundle Resources

The resources that the project comes with are stored in the. Bundle and these resources are read-only and cannot be written

NSString *defaultdbpath = [[Nsbundlemainbundle] resourcepath];

Read Library Directory

Nsarray *paths = nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES);  NSString *path = [paths Objectatindex:0];  NSLog (@ "%@      

iOS Learning Note 22: Files created by the running iOS app

Related Article

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.