Skip to content

Instantly share code, notes, and snippets.

@erynofwales
Created September 10, 2011 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erynofwales/1207898 to your computer and use it in GitHub Desktop.
Save erynofwales/1207898 to your computer and use it in GitHub Desktop.
Creating temporary directories in Cocoa/Objective-C
// 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