10

My app is sandboxed (as per the latest App Store guidelines), and I want to create some temporary files.

Am I allowed to do so? If "yes", WHERE am I allowed to do it? Is there any prespecified path? (And a command to access that path?)

jscs
  • 63,694
  • 13
  • 151
  • 195
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223

2 Answers2

15

You should use the NSTemporaryDirectory() function, which will find and return the appropriate temporary folder for your application (regardless of sandbox status, OS version, and a host of other things). Take a look at this Cocoa With Love post for much more detail about NSTemporaryDirectory() and other temporary directory-related details.

Tim
  • 59,527
  • 19
  • 156
  • 165
5

There is a good article about temporary directories on NSHipster:

http://nshipster.com/nstemporarydirectory/

The author suggests this code which is working perfectly with sandboxed apps as well:

NSError *error;
NSString *globallyUniqueString = [[NSProcessInfo processInfo] globallyUniqueString];
NSString *tempDirectoryPath = [NSTemporaryDirectory() stringByAppendingPathComponent:globallyUniqueString];
NSURL *tempDirectoryURL = [NSURL fileURLWithPath:tempDirectoryPath isDirectory:YES];
[[NSFileManager defaultManager] createDirectoryAtURL:tempDirectoryURL withIntermediateDirectories:YES attributes:nil error:&error];
Flovdis
  • 2,945
  • 26
  • 49