【发布时间】: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。