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

来源:互联网 发布:算法工程师学什么专业 编辑:程序博客网 时间:2024/05/22 10:52

#pragma mark -  Excluding a File from Backups oniOS 5.1


- (BOOL)addSkipBackupAttributeToItemAtURL_iOS5_1:(NSURL*)URL

{

   assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    

    NSError *error = nil;

   BOOLsuccess = [URLsetResourceValue: [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];

    

   constchar* 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:strDicwithIntermediateDirectories:YESattributes:nil error:nil];

    }

    

    urlDefault = [NSURL fileURLWithPath:strDic];

    

    if (fVersion > 5.0 && fVersion < 5.1)

       [selfaddSkipBackupAttributeToItemAtURL:urlDefault];

    else if(fVersion >= 5.0)

       [selfaddSkipBackupAttributeToItemAtURL_iOS5_1:urlDefault];

    

    return urlDefault;

}


icloud备份,这个东西比较烦人,但从用户的角度,好多东西确实没必要备份。


Apps must follow the iOS Data Storage Guidelines or they will berejected

2.23

We found that your app does not follow the iOS Data StorageGuidelines, which is required per the App Store ReviewGuidelines.

In particular, we found that on launch and/or content download,your app stores 13.4 MB. To check how much data your app isstoring:

- Install and launch your app
- Go to Settings > iCloud > Storage & Backup > ManageStorage 
- If necessary, tap "Show allapps" 
- Check your app's storage

The iOS Data Storage Guidelines indicate that only content that theuser creates using your app, e.g., documents, new files, edits,etc., may be stored in the /Documents directory - and backed up byiCloud. 

Temporary files used by your app should only be stored in the /tmpdirectory; please remember to delete the files stored in thislocation when the user exits the app.

Data that can be recreated but must persist for proper functioningof your app - or because customers expect it to be available foroffline use - should be marked with the "do not back up" attribute.For NSURL objects, add the NSURLIsExcludedFromBackupKey attributeto prevent the corresponding file from being backed up. ForCFURLRef objects, use the correspondingkCFURLIsExcludedFromBackupKeyattribute. 

For more information, please see Technical Q&A 1719: How do Iprevent files from being backed up to iCloud andiTunes?.

It is necessary to revise your app to meet the requirements of theiOS 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 andiTunes?

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

A: On iOS, apps are responsible for ensuring thatonly user data and not application data is backed up to iCloud andiTunes. The exact steps necessary vary between iOS version, so thisQA will describe the process for each version of iOS. For moreinformation on exactly what data should or should not be backed up,see the App Backup Best Practices section ofthe iOS App Programming Guide.

Important: Apps should avoidmingling app data and user data in the same file. Doing so willunnecessarily increase backup sizes and can be considered aviolation of the iOS Data Storage Guidelines.

 

iOS 5.1 and later

Starting in iOS 5.1, apps can useeither NSURLIsExcludedFromBackupKey orkCFURLIsExcludedFromBackupKey fileproperties to exclude files from backups. Either of these APIs ispreferred over the older, deprecated approach of directly settingan extended attribute. All apps running on iOS 5.1 should use theseAPIs to exclude files from backups.

Listing1  Excluding a File fromBackups 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 followingmethod to set the "do not back up" extended attribute. Whenever youcreate a file or folder that should not be backed up, write thedata to the file and then call this method, passing in a URL to thefile.

Warning: The code that followshas been deprecated and should only be used on iOS 5.0.1 orearlier. When running in iOS 5.1, apps should usethe NSURL and CFURL keysdescribed above.

 

Listing2  Setting the ExtendedAttribute 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 yourapp must support iOS 5.0, then you will need to store your app datain Caches toavoid that data being backed up. iOS will delete your files fromthe Caches directorywhen necessary, so your app will need to degrade gracefully if it'sdata files are deleted.

Back to Top


DocumentRevision History


DateNotes2012-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 beingbacked up to iCloud and iTunes.



0 0