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
По дяволите с apple... те дори не връщат грешка за това и резултатът ще бъде ДА. Но това няма да зададе 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

Swift:

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