将 NSURLIsExcludedFromBackupKey 应用于 iPhone 上的服务器 URL 或目录 URL?

Posted

技术标签:

【中文标题】将 NSURLIsExcludedFromBackupKey 应用于 iPhone 上的服务器 URL 或目录 URL?【英文标题】:Apply NSURLIsExcludedFromBackupKey to Server URL or URL of Directory on iPhone? 【发布时间】:2013-11-12 22:09:05 【问题描述】:

由于我如何在 iPhone 上保存一些下载的文件,我正在与应用程序拒绝作斗争。

我已经实现了NSURLIsExcludedFromBackupKey,但我不清楚我是否正确地执行了它。我应该将NSURLIsExcludedFromBackupKey 应用于代表服务器上文件的 NSURL,还是以某种方式将其应用于 iPhone 上保存文件的目录?

这是我现在在应用被拒绝后创建的:

// Get / Create the File Directory on the iPhone 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyDirectory"];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 

// Setup the NSURL where the PNG is located
NSURL *imageURL = [NSURL URLWithString:@"http://www.myserver.com/image.png"];

NSError *err = nil; // Exclude This Image from the iCloud backup system
BOOL excluded = [imageURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&err];

if (!excluded) 
        //NSLog(@"Failed to exclude from backup");
 else 
    //NSLog(@"Excluding from backup"); // this works...

// Create the UIImage
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];

// Data about the downloaded image
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];

if ([data1 writeToFile:pngFilePath atomically:YES] && [data1 writeToFile:pngFilePathRetina atomically:YES]) 
        // Saved to phone
 else         
        // Did not save to phone

【问题讨论】:

由于 ios 不备份远程文件,因此您可以确定 URL 必须是位于应用沙箱中的文件的文件 URL。 【参考方案1】:

这是我最终如何对其进行编码以使其获得批准的方式。这可能有点矫枉过正,但总比花一个月的时间去猜测究竟如何获得批准要好。

 // Get / Create the File Directory on the iPhone 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyDirectory"];

// if the directory does not yet exist on the phone, create it
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 


    // Assign NSURLIsExcludedFromBackupKey to the directory at dataPath, so is does not backup to iCloud
    NSURL *directoryUrl = [NSURL fileURLWithPath:dataPath isDirectory:YES];
    NSError *err1 = nil;
    BOOL excluded1 = [directoryUrl
                         setResourceValue:[NSNumber numberWithBool:YES]
                         forKey:NSURLIsExcludedFromBackupKey
                         error:&err1];

    if (!excluded1) 
            //NSLog(@"Failed to exclude directory from backup");
     else 
            //NSLog(@"Excluding directory from backup");
    



    // Setup the NSURL where the PNG, which will be downloaded, is located 
    NSURL *imageURL = [NSURL URLWithString:@"http://www.myserver.com/image.png"];

    NSError *err2 = nil; // Exclude This Image from the iCloud backup system, Not sure if I need this or not…
    BOOL excluded2 = [imageURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&err2];

    if (!excluded2) 
        //NSLog(@"Failed to exclude from backup");
     else 
        //NSLog(@"Excluding from backup"); // this works...
    


   // Create the UIImage
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];

    // Data about the downloaded image
    NSData *downloadedImageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

    // Path for where we are going to save the image
    NSString *storePath = [NSString stringWithFormat:@"%@/myimage.png", dataPath]; 

    if ([downloadedImageData writeToFile:storePath atomically:YES]) 

  // the Image is saved to the phone

  // NSURL representing the image that is now saved on the phone          
  NSURL *imageOnPhone = [NSURL fileURLWithPath:storePath isDirectory:YES];

        // Exclude this image, which is now stored on the phone, from iCloud Backup
        NSError *err3 = nil;
        BOOL excluded3 = [imageOnPhone
                                  setResourceValue:[NSNumber numberWithBool:YES]
                                  forKey:NSURLIsExcludedFromBackupKey
                                  error:&err3];
            if (!excluded3) 
                //NSLog(@"Failed to exclude from backup (standard)");
             else 
                //NSLog(@"Excluding from backup (standard)");
            

     else         
            // Did not save to phone
    

【讨论】:

与其浪费时间和混乱检查目录是否存在,不如每次尝试创建它更快更简单 如果设置资源值失败,为了调试目的,注销它失败的错误会更有用 不要费心为您的远程 URL 设置NSURLIsExcludedFromBackupKey;它永远不会成功 您的代码会下载一个图像(同步,这很糟糕),从中创建一个UIImage,然后将其转换回PNG。几乎可以肯定,最好下载图像数据并直接存储,而无需尝试对 PNG 进行编码 [NSString stringWithFormat:@"%@/myimage.png", dataPath] — 不要这样做。请改用stringByAppendingPathComponent: 方法,如代码开头附近所示。更好的是,使用 URL 开头,而不是路径【参考方案2】:

为了更简洁地回答你原来的问题:

您应该将NSURLIsExcludedFromBackupKey 应用于目的地。即您不想备份的文件或目录。

iPhone 不知道您的应用可能正在使用哪些远程文件,因此不可能尝试备份它们。更重要的是,如果您尝试在此类 URL 上设置 NSURLIsExcludedFromBackupKey,您很快就会发现由于 URL 不受支持而失败。

【讨论】:

以上是关于将 NSURLIsExcludedFromBackupKey 应用于 iPhone 上的服务器 URL 或目录 URL?的主要内容,如果未能解决你的问题,请参考以下文章

如何将Ios文件上传到

Javascript 将正则表达式 \\n 替换为 \n,将 \\t 替换为 \t,将 \\r 替换为 \r 等等

如何将视频文件转换格式

sh 一个将生成CA的脚本,将CA导入到钥匙串中,然后它将创建一个证书并与CA签名,然后将其导入到

python怎么将0写入文件?

如何将CMD窗口背景改成透明?