Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Can't save file to 'Application Support' folder, but can to 'Documents'

I can download and save a binary file to the 'Documents' folder with a custom name perfectly fine.

If I just change the URL to the 'Application Support' folder instead of the 'Documents' folder, it fails to write to that URL saying it doesn't exist.

Here's the URL construction code:

- ( NSURL * ) getSaveFolder {     NSURL * appSupportDir    = nil;     NSURL * appDirectory     = nil;     NSArray * possibleURLs   = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSAllDomainsMask];          if ( [possibleURLs count] >= 1 )     {         appSupportDir = [possibleURLs objectAtIndex:0];     }      if ( appSupportDir != nil)     {         NSString * appBundleID = [[NSBundle mainBundle] bundleIdentifier];         appDirectory           = [appSupportDir URLByAppendingPathComponent:appBundleID];     }      return appSupportDir; } 

Here's the saving code:

- ( void ) writeOutDataToFile:( NSData * )data {     NSURL * finalURL = [self.rootPathURL URLByAppendingPathComponent:self.aFileName];      [data writeToURL:finalURL atomically:YES]; } 

If I change the NSArray to:

NSArray * possibleURLs   = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]; 

then it saves fine.

I've read the Apple Docs on File stuff and can't fix this - what am I missing?

like image 631
iOSProgrammingIsFun Avatar asked Apr 25 '13 01:04

iOSProgrammingIsFun


2 Answers

In case anyone is unsure how to do what rmaddy describes:

NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject]; //If there isn't an App Support Directory yet ...  if (![[NSFileManager defaultManager] fileExistsAtPath:appSupportDir isDirectory:NULL]) {     NSError *error = nil; //Create one      if (![[NSFileManager defaultManager] createDirectoryAtPath:appSupportDir withIntermediateDirectories:YES attributes:nil error:&error]) {         NSLog(@"%@", error.localizedDescription);     }     else { // *** OPTIONAL *** Mark the directory as excluded from iCloud backups          NSURL *url = [NSURL fileURLWithPath:appSupportDir];         if (![url setResourceValue:@YES                             forKey:NSURLIsExcludedFromBackupKey                              error:&error])         {             NSLog(@"Error excluding %@ from backup %@", url.lastPathComponent, error.localizedDescription);         }         else {             NSLog(@"Yay");         }     } } 
like image 174
0xWood Avatar answered Sep 23 '22 13:09

0xWood


Unlike the Documents directory, the Application Support directory does not exist in the app's sandbox by default. You need to create it before you can use it.

And a much simpler way to get a reference to the directory is:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); NSString *appSupportDirectory = paths.firstObject; 
like image 24
rmaddy Avatar answered Sep 22 '22 13:09

rmaddy