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

#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 rejected

2.23

We 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 storage

The 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.html

 

Technical 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

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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值