31

I am trying to download image files and store in NSDocumentDirectory. In order to do so, I has to turn off data backup on iCloud and iTunes. Below are my codes:

+(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];
}

and for my 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;
   }
}

However, the BOOL result = [fileURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error]; created the following message

CFURLSetResourcePropertyForKey failed because it was passed this URL which has no scheme: /var/mobile/Applications/CF69D567-1D37-4053-BFA8-5D0FCBD9C2B2/Documents/coffee.jpg

I'm just wondering if any encountered this problem??

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
hook38
  • 3,899
  • 4
  • 32
  • 52
  • Hell with apple... they don't even return an error on this and the result will be YES. But this will not set the "ExcludedFromBackupKey". They rejected me twice for this :( – Nikita P Feb 04 '15 at 07:18

3 Answers3

135

Solved. once I changed

NSURL *fileURL = [NSURL URLWithString:localFilePath];

to

NSURL *fileURL = [NSURL fileURLWithPath:localFilePath];

everything work perfectly.

hook38
  • 3,899
  • 4
  • 32
  • 52
0

Swift:

let fileURL = URL(fileURLWithPath: somepath)
Hemang
  • 26,840
  • 19
  • 119
  • 186
-1

The designated method for creating a local file's url is : NSURL * __url = [[NSURL alloc] initFileURLWithPath:__path isDirectory:NO]; It works perfectly. Good luck!

  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/34913688) – Ben A. Aug 30 '23 at 17:18