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
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.
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
Back to Top
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
described above.Listing
2 Setting the Extended
Attribute on iOS 5.0.1
Back to Top
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
avoid that data being backed up. iOS will delete your files from
the
when necessary, so your app will need to degrade gracefully if it's
data files are deleted.
Back to TopDocument
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 thatonly 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 useeither 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; |
} |
iOS 5.0.1
If your app must support iOS 5.0.1, you can use the followingmethod 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
NSURLand
CFURLkeys
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; |
} |
iOS 5.0
It is not possible to exclude data from backups on iOS 5.0. If yourapp must support iOS 5.0, then you will need to store your app data
in
Cachesto
avoid that data being backed up. iOS will delete your files from
the
Cachesdirectory
when necessary, so your app will need to degrade gracefully if it's
data files are deleted.
Back to Top
Document
Revision History
Date | Notes |
---|---|
2012-04-23 | Updated 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. |
相关文章推荐
- iOS下载文件,保存路径. 防止加到iCloud备份
- iOS文件,保存路径. 防止加到iCloud备份
- winform WebBrowser 文件下载拦截 获取文件本地保存路径
- iOS 将服务器下载下来的db文件和本地的db文件合并后保存
- ios - 文件保存路径的获取
- 下载文件,并弹出保存提示框,选择位置,绝对路径 or相对路径
- Android中调用系统摄像并且保存到指定位置的一些问题&Uri转文件路径方法
- ios 将数据写入到文件
- C语言 HTTP 下载文件
- 拍照并保存到本地相册中 ios
- iOS开发网络篇—文件下载(四·暂停和恢复)
- 从给定网页上下载<img src="" />格式的图片,并保存到制指定路径
- iOS 将图片保存到App中,并取…
- iOS 保存到相册(视频,图片)
- iOS 开发之网络——文件上传&文件下载
- Unity商店下载的文件保存路径?
- &lt;转&gt;&nbsp;iOS开发网络篇—文件的上传
- iOS开发网络篇—文件下载(四·暂停和恢复)
- JavaEE 保存文件获取绝对路径getResource("")和servletContext.getRealPath("/")
- Asset Store 资源上电视下载的文件保存路径如下: