Ошибка CFURLSetResourcePropertyForKey при отключении резервного копирования данных в NSDocumentDirectory

Я пытаюсь загрузить файлы изображений и сохранить их в 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];
}

и для моего addskipbackupattributetoitematurl:

+(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 numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; создал следующее сообщение

Ошибка CFURLSetResourcePropertyForKey, поскольку был передан этот URL-адрес без схемы: /var/mobile/Applications/CF69D567-1D37-4053-BFA8-5D0FCBD9C2B2/Documents/coffee.jpg

Мне просто интересно, кто-нибудь сталкивался с этой проблемой??


person hook38    schedule 17.10.2012    source источник
comment
Черт с яблоком ... они даже не возвращают ошибку, и результат будет ДА. Но это не установит ExcludedFromBackupKey. Дважды мне за это отказали :(   -  person Nikita P    schedule 04.02.2015


Ответы (2)


Решено. однажды я изменился

NSURL *fileURL = [NSURL URLWithString:localFilePath];

to

NSURL *fileURL = [NSURL fileURLWithPath:localFilePath];

все работает отлично.

person hook38    schedule 17.10.2012
comment
Это спасло меня еще от двух часов упорного труда! Благодарю вас! ???? - person SexyBeast; 16.04.2015
comment
Вы спасли мою жизнь! - person Hwangho Kim; 29.06.2016

Свифт:

let fileURL = URL(fileURLWithPath: somepath)
person Hemang    schedule 12.09.2017