您的位置:首页 > 移动开发 > IOS开发

iOS下载文件,保存路径. 防止…

2014-01-23 17:31 585 查看
#pragma mark - Excluding a File from Backups on
iOS 5.1- (BOOL)addSkipBackupAttributeToItemAtURL_iOS5_1:(NSURL
*)URL{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);NSError *error = nil;BOOL
success = [URL
setResourceValue: [NSNumber numberWithBool: YES]forKey: NSURLIsExcludedFromBackupKey error: &error];if(!success){NSLog(@"Error excluding %@ from backup
%@", [URL lastPathComponent], error);}return success;}#pragma mark - Setting the Extended Attribute on iOS 5.0.1#import- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL
*)URL{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);const char* filePath = [[URL path] fileSystemRepresentation];const
char* attrName =
"com.apple.MobileBackup";u_int8_t attrValue = 1;int result = setxattr(filePath, attrName,
&attrValue, sizeof(attrValue), 0, 0);return result == 0;}- (NSURL *)getDefaultDir{
NSURL *urlDefault = nil;NSString *strDic = nil;NSString *strVersion = [[UIDevice currentDevice] systemVersion];float fVersion = 0.0;if(strVersion.length > 0)fVersion = [strVersion floatValue];if (fVersion == 5.0) {strDic = [NSString stringWithFormat:@"%@/Library/Caches/",NSHomeDirectory()];} else {strDic = [NSString stringWithFormat:@"%@/Library/%@/",NSHomeDirectory(),[[NSBundle mainBundle] bundleIdentifier]];}if
(![[NSFileManager defaultManager] fileExistsAtPath:strDic]) {[[NSFileManager defaultManager] createDirectoryAtPath:strDic
withIntermediateDirectories:YES
attributes:nil error:nil];}urlDefault = [NSURL fileURLWithPath:strDic];if (fVersion > 5.0 && fVersion < 5.1)[self
addSkipBackupAttributeToItemAtURL:urlDefault];else if
(fVersion >= 5.0)[self
addSkipBackupAttributeToItemAtURL_iOS5_1:urlDefault];return urlDefault;}icloud备份,这个东西比较烦人,但从用户的角度,好多东西确实没必要备份。Apps must follow the iOS Data Storage Guidelines or they will be
rejected2.23We found that your app does not follow the iOS Data Storage
Guidelines, which is required per the App Store Review
Guidelines.In particular, we found that on launch and/or content download,
your app stores 13.4 MB. To check how much data your app is
storing:- Install and launch your app- Go to Settings > iCloud > Storage & Backup > Manage
Storage- If necessary, tap "Show all
apps"- Check your app's storageThe iOS Data Storage Guidelines indicate that only content that the
user creates using your app, e.g., documents, new files, edits,
etc., may be stored in the /Documents directory - and backed up by
iCloud.Temporary files used by your app should only be stored in the /tmp
directory; please remember to delete the files stored in this
location when the user exits the app.Data that can be recreated but must persist for proper functioning
of your app - or because customers expect it to be available for
offline use - should be marked with the "do not back up" attribute.
For NSURL objects, add the NSURLIsExcludedFromBackupKey attribute
to prevent the corresponding file from being backed up. For
CFURLRef objects, use the corresponding
kCFURLIsExcludedFromBackupKey
attribute.For more information, please see Technical Q&A 1719: How do I
prevent files from being backed up to iCloud and
iTunes?.It is necessary to revise your app to meet the requirements of the
iOS Data Storage Guidelines.(说的很明白了,
让你把离线数据加上离线的属性 )解决方案:file:///Users/bjrd_mac/Library/Developer/Shared/Documentation/DocSets/com.apple.adc.documentation.AppleiOS6.0.iOSLibrary.docset/Contents/Resources/Documents/index.html#qa/qa1719/_index.htmlTechnical Q&A QA1719


How do I prevent files from being backed up to iCloud and
iTunes?


Q: My app has a number of files that need to be
stored on the device permanently for my app to function properly
offline. However, those files do not contain user data and don't
need to be backed up. How can I prevent them from being backed
up?

A: On iOS, apps are responsible for ensuring that
only user data and not application data is backed up to iCloud and
iTunes. The exact steps necessary vary between iOS version, so this
QA will describe the process for each version of iOS. For more
information on exactly what data should or should not be backed up,
see the App Backup Best Practices section of
the iOS App Programming Guide.Important: Apps should avoid
mingling app data and user data in the same file. Doing so will
unnecessarily increase backup sizes and can be considered a
violation of the iOS Data Storage Guidelines.


iOS 5.1 and later

Starting in iOS 5.1, apps can use
either NSURLIsExcludedFromBackupKey orkCFURLIsExcludedFromBackupKey file
properties to exclude files from backups. Either of these APIs is
preferred over the older, deprecated approach of directly setting
an extended attribute. All apps running on iOS 5.1 should use these
APIs to exclude files from backups.Listing
1
Excluding a File from
Backups on iOS 5.1
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
NSError *error = nil;
BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
}
return success;
}
Back to Top


iOS 5.0.1

If your app must support iOS 5.0.1, you can use the following
method to set the "do not back up" extended attribute. Whenever you
create a file or folder that should not be backed up, write the
data to the file and then call this method, passing in a URL to the
file.Warning: The code that follows
has been deprecated and should only be used on iOS 5.0.1 or
earlier. When running in iOS 5.1, apps should use
the
NSURL
and
CFURL
keys
described above.Listing
2
Setting the Extended
Attribute on iOS 5.0.1
#import
- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}
Back to Top


iOS 5.0

It is not possible to exclude data from backups on iOS 5.0. If your
app must support iOS 5.0, then you will need to store your app data
in
Caches
to
avoid that data being backed up. iOS will delete your files from
the
Caches
directory
when necessary, so your app will need to degrade gracefully if it's
data files are deleted.
Back to Top

Document
Revision History

DateNotes
2012-04-23Updated for iOS 5.1
2011-11-10-Fixed critical bug in code snippet.
New document that describes how an app can prevent files from being
backed up to iCloud and iTunes.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: