Create temp file for a sandboxed Cocoa application

僤鯓⒐⒋嵵緔 提交于 2019-12-10 02:56:33

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/12367512/create-temp-file-for-a-sandboxed-cocoa-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!