Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将NsurLiseCludedFromBackupKey应用于服务器URL或iPhone上目录的URL?_Ios_Iphone_Backup - Fatal编程技术网

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

Ios 将NsurLiseCludedFromBackupKey应用于服务器URL或iPhone上目录的URL?,ios,iphone,backup,Ios,Iphone,Backup,由于我在iPhone上保存了一些下载的文件,我正在与一个被拒绝的应用程序作斗争 我已经从备份键实现了nsurlisecluded,但我不清楚是否正确。我应该将nsurlisecludedfrombackupkey应用于表示服务器上文件的NSURL,还是将其应用于iPhone上保存文件的目录 以下是我在应用程序被拒绝后创建的内容: // Get / Create the File Directory on the iPhone NSArray *paths = NSSearchPathForDi

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

我已经从备份键实现了
nsurlisecluded
,但我不清楚是否正确。我应该将
nsurlisecludedfrombackupkey
应用于表示服务器上文件的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
}

下面是我如何编码它,使它得到批准的。这可能有点矫枉过正,但总比花一个月的时间猜测如何获得批准要好

 // 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
    }

要更简洁地回答您的原始问题:

您应该将
nsurlisecludedfrombackupkey
应用到目标。i、 e.不希望备份的文件或目录


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

因为iOS不备份远程文件,您可以确定URL必须是位于应用程序沙盒中的文件的文件URL。与其浪费时间和混乱地检查目录是否存在,不如在每次设置资源值失败时尝试创建它,这样会更快更简单,出于调试目的,注销失败的错误会更有用,不要费心为远程URL设置
nsurlisecludedfrombackupkey
;它永远不会成功。您的代码下载图像(同步,这是不好的),从中创建
UIImage
,然后将其转换回PNG。几乎可以肯定,您最好下载图像数据并直接存储,而不需要尝试编码PNG
[NSString stringWithFormat:@“%@/myimage.PNG”,dataPath]
-不要这样做。请改用
stringByAppendingPathComponent:
方法,如代码开头所示。更妙的是,使用URL开始,而不是路径