Created
September 10, 2011 03:58
-
-
Save erynofwales/1207898 to your computer and use it in GitHub Desktop.
Creating temporary directories in Cocoa/Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a unique temporary direction | |
// This procedure is safe from a number of potential problems including | |
// concurrency, file permission, security, and persistence issues | |
// Taken from http://cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html | |
NSString *tmpdirTemplate = [NSTemporaryDirectory() | |
stringByAppendingPathComponent:@"app.XXXXXX"]; | |
const char *tmpdirTemplateCStr = [tmpdirTemplate fileSystemRepresentation]; | |
char *tmpdirCStr = (char *) malloc(strlen(tmpdirTemplateCStr) + 1); | |
strcpy(tmpdirCStr, tmpdirTemplateCStr); | |
char *result = mkdtemp(tmpdirCStr); | |
if (!result) { | |
// TODO: Handle case that mkdtemp fails | |
} | |
NSString *tempDirectoryPath = [[NSFileManager defaultManager] | |
stringWithFileSystemRepresentation:tmpdirCStr | |
length:strlen(tmpdirCStr)]; | |
free(tmpdirCStr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment