I am using this code:
+ (NSString *)getUniqueFilenameInFolder:(NSString *)folder forFileExtension:(NSString *)fileExtension { NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *existingFiles = [fileManager contentsOfDirectoryAtPath:folder error:nil]; NSString *uniqueFilename; do { CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault); CFStringRef newUniqueIdString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueId); uniqueFilename = [[folder stringByAppendingPathComponent:(NSString *)newUniqueIdString] stringByAppendingPathExtension:fileExtension]; CFRelease(newUniqueId); CFRelease(newUniqueIdString); } while ([existingFiles containsObject:uniqueFilename]); return uniqueFilename; }
Maybe this helps someone. :-)
Keep in mind that this returns the full path to the unique file. You might want to add small changes to simply return the file name. For example, if you want to save it somewhere.
thomas
source share