简体   繁体   中英

Create temp file for a sandboxed Cocoa application

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?)

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM