Last Updated: February 25, 2016
·
443
· jarsen

Easy Unique File Names

A method for quickly making sure that the file URL you're about to write to is unique. If the file already exists it simply inserts a UUID. You can throw this method in somewhere or create a category on NSFileManager.

- (NSURL *)uniqueFileURLWithURL:(NSURL *)url
{    
    if (![self fileExistsAtPath:url.path]) {
        return url;
    }

    NSString *uniquePath = [NSString stringWithFormat:@"%@%@%@", [url.path stringByDeletingPathExtension], [[NSProcessInfo processInfo] globallyUniqueString], url.pathExtension];
    return [NSURL fileURLWithPath:uniquePath];
}

I got the inspiration from this StackOverflow question.