addSkipBackupAttributeToItemAtURL 不适用于 iOS7

Posted

技术标签:

【中文标题】addSkipBackupAttributeToItemAtURL 不适用于 iOS7【英文标题】:addSkipBackupAttributeToItemAtURL not working with iOS7 【发布时间】:2014-01-16 07:06:48 【问题描述】:

我的 ios 应用程序刚刚被拒绝,因为该应用程序将数据存储在 Documents 上,因此它由 iCloud 备份,这是不允许的,因为数据是从服务器下载的。 但即使我使用 addSkipBackupAttributeToItemAtURL 它仍然显示为 iCloud 的备份。这里是我使用过的代码

    NSError *error = nil;
    NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
    if(!error)
    
       [self HidePreLoader];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
         NSLog(@"_paths--->%@",paths);
        NSString *path = [paths objectAtIndex:0];
         NSLog(@"_path---->%@",path);
        NSString *dataPath = [path stringByAppendingPathComponent:@"/MyFolder"];
         NSLog(@"_dataPath---->%@",dataPath);
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
        
        NSString *zipPath = [dataPath stringByAppendingPathComponent:downfilename];
        [data writeToFile:zipPath options:0 error:&error];
        if(!error)
        
            [self HidePreLoader];
            ZipArchive *za = [[ZipArchive alloc] init];
            if ([za UnzipOpenFile: zipPath]) 
                BOOL ret = [za UnzipFileTo: dataPath overWrite: YES];
                if (NO == ret) [za UnzipCloseFile];
                 NSLog(@"folderPath--->%@",folderPath);
   //Here i have used the use code
                NSURL *guidesURL = [NSURL fileURLWithPath:folderPath];
                [guidesURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:NULL];
  //The following code also doesnt work
  //NSURL *guidesURL = [NSURL fileURLWithPath:folderPath];
  //[self addSkipBackupAttributeToItemAtURL:guidesURL];

                  [self HidePreLoader];
                NSString *path = nil;
                if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
                
                    path = [folderPath stringByAppendingPathComponent:@"module-B1-ipadmain-swipe-tablet.html"];
                
                else
                
                    path = [folderPath stringByAppendingPathComponent:@"module-B1-ipadmain-swipe-phone.html"];
                

                NSLog(@"path--->%@",path);

                dispatch_async(dispatch_get_main_queue(), ^

                    appDel.currentReadingPlanWithPath = path;
                    appDel.moduleDownloaded = YES;
                    [appDel SetCurrentDownloadPath];
                    NSLog(@"file download success");
                    [progview setHidden:YES];
                    [progval setHidden:YES];
                    [self HidePreLoader];
                    downloadView.userInteractionEnabled=YES;
                    [self SetModuleDownloaded:@"true"];
                    [self ShowAlert:@"Download" message:@"Success"];
                     [self HidePreLoader];
                                    );
                 [self HidePreLoader];
            
        
        else
        
            NSLog(@"Error saving file %@",error);
        
    
    else
    
        NSLog(@"Error downloading zip file: %@", error);
    

);

AddSkipBackupAttributeToItemAtURL 方法

-(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;

当我尝试上面的代码时,iCloud 仍然显示 iOS7 中的应用程序。请帮我解决这个问题。

【问题讨论】:

我不知道,但是文档说 “通常对用户文档进行的某些操作会导致此属性重置为 false...”,所以这就是我要检查的第一件事。 【参考方案1】:

使用“addSkipBackupAttributeToItemAtURL”功能前请检查iOS版本。

还请关注此网址: https://developer.apple.com/library/ios/qa/qa1719/_index.html 和这个网址 https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/PerformanceTuning/PerformanceTuning.html#//apple_ref/doc/uid/TP40007072-CH8-SW9

【讨论】:

以上是关于addSkipBackupAttributeToItemAtURL 不适用于 iOS7的主要内容,如果未能解决你的问题,请参考以下文章