gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:29:54 27 4
gpt4 key购买 nike

由于我在 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
}

最佳答案

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

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

关于ios - 将 NSURLIsExcludedFromBackupKey 应用于 iPhone 上的服务器 URL 或目录 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19940964/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com