Ios 在NSDocumentDirectory上禁用数据备份时,CFURLSetResourcePropertyWorkey失败

Ios 在NSDocumentDirectory上禁用数据备份时,CFURLSetResourcePropertyWorkey失败,ios,nsdocumentdirectory,Ios,Nsdocumentdirectory,我正在尝试下载图像文件并存储在NSDocumentDirectory中。为此,我必须关闭iCloud和iTunes上的数据备份。以下是我的代码: +(void)saveData:(NSData*)thedata:(NSString*)fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *document

我正在尝试下载图像文件并存储在
NSDocumentDirectory
中。为此,我必须关闭iCloud和iTunes上的数据备份。以下是我的代码:

+(void)saveData:(NSData*)thedata:(NSString*)fileName
{
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
   NSFileManager *fileManager = [NSFileManager defaultManager];
   [fileManager createFileAtPath:localFilePath contents:thedata attributes:nil];
   //prevent files from backup on iCloud or iTune
   NSURL *fileURL = [NSURL URLWithString:localFilePath];
   [self addSkipBackupAttributeToItemAtURL:fileURL];
}
对于我的AddSkipBackupAttributeToItemAttribute:

+(BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)fileURL
{
   if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]])
   {
       NSLog(@"File %@ doesn't exist!",[fileURL path]);
       return NO;
   }
   NSString *currSysVer = [[UIDevice currentDevice] systemVersion];    
   if ([currSysVer isEqualToString:@"5.0.1"])
   {
       const char* filePath = [[fileURL path] fileSystemRepresentation];
       const char* attrName = "com.apple.MobileBackup";
       u_int8_t attrValue = 1;
       int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
       NSLog(@"Excluded '%@' from backup",fileURL);
       return result == 0;
   }
   else if (&NSURLIsExcludedFromBackupKey)
   {
       NSError *error = nil;
       BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
       if (result == NO)
       {
           NSLog(@"Error excluding '%@' from backup. Error: %@",fileURL, error);
           return NO;
       }
       else
       { 
           NSLog(@"Excluded '%@' from backup",fileURL);
           return YES;
       }
   }
   else
   {
       return YES;
   }
}
但是,
BOOL result=[fileURL setResourceValue:[NSNumber numberwhithbool:YES]forKey:nsurlisecludedfrombackupkey错误:&error]创建了以下消息

cfurlsetResourcePropertyWorkey失败,因为它传递了此URL,而此URL没有方案:/var/mobile/Applications/CF69D567-1D37-4053-BFA8-5D0FCBD9C2B2/Documents/coffee.jpg


我只是想知道是否有人遇到了这个问题???

解决了。一旦我改变了

NSURL *fileURL = [NSURL URLWithString:localFilePath];


一切都很完美。

斯威夫特:

let fileURL = URL(fileURLWithPath: somepath)

该死的苹果。。。他们甚至不会在这个问题上返回错误,结果将是肯定的。但这不会设置“ExcludedFromBackupKey”。他们因为这个拒绝了我两次:(这救了我两个多小时的苦役!谢谢!你救了我的命!
let fileURL = URL(fileURLWithPath: somepath)