Skip to content

Instantly share code, notes, and snippets.

@Abizern
Created January 22, 2012 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abizern/1657624 to your computer and use it in GitHub Desktop.
Save Abizern/1657624 to your computer and use it in GitHub Desktop.
A category on NSURL that returns a properly encoded URL for a file in the NSUserDomainMask
//
// NSURL+ADNFileHelpers.h
//
// Created by Abizer Nasir on 02/08/2011.
//
// Public Domain because I love you
#import <Foundation/Foundation.h>
@interface NSURL (ADNFileHelpers)
+ (NSURL *)adnURLForFileName:(NSString *)fileName inDirectory:(NSSearchPathDirectory)searchDirectory;
+ (NSURL *)adnURLInDocumentDirectoryForFilename:(NSString *)fileName;
+ (NSURL *)adnURLInCachesDirectoryForFilename:(NSString *)fileName;
+ (NSURL *)adnURLInDownloadsDirectoryForFilename:(NSString *)filename;
@end
//
// NSURL+ADNFileHelpers.m
//
// Created by Abizer Nasir on 02/08/2011.
//
// Public Domain because I love you
#import "NSURL+ADNFileHelpers.h"
@implementation NSURL (ADNFileHelpers)
#pragma mark - Main Extension functino
+ (NSURL *)adnURLForFileName:(NSString *)fileName inDirectory:(NSSearchPathDirectory)searchDirectory {
NSString *percentEscapedFileName = [fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *URLForDirectory = [[fileManager URLsForDirectory:searchDirectory inDomains:NSUserDomainMask] lastObject];
return [NSURL URLWithString:percentEscapedFileName relativeToURL:URLForDirectory];
}
#pragma mark - Convenience functions for iOS apps
+ (NSURL *)adnURLInDocumentDirectoryForFilename:(NSString *)fileName {
return [self adnURLForFileName:fileName inDirectory:NSDocumentDirectory];
}
+ (NSURL *)adnURLInCachesDirectoryForFilename:(NSString *)fileName {
return [self adnURLForFileName:fileName inDirectory:NSCachesDirectory];
}
+ (NSURL *)adnURLInDownloadsDirectoryForFilename:(NSString *)filename {
return [self adnURLForFileName:filename inDirectory:NSDownloadsDirectory];
}
@end
@mikeabdullah
Copy link

Come to think of it, why not just do:

return [URLForDirectory URLByAppendingPathComponent:fileName];

instead, Abizer?

@Motti-Shneor
Copy link

One little service is missing in this category --- dealing with slashes/colons in the fileName.
In my case, I try to name (programmatically) a series of exported .csv file by some prefix, and short-formatted date (which usually looks like 21/3/1955). So…

I let the user select a single directory (using the NSOpenPanel/NSSavePanel), retrieve the directory URL and append the "fileName" component programmatically.

Until now I haven't found any NSString, NSURL, NSPAthUtility or NSFileManager utility method that will correctly handle the slashes in the suggested file name (which usually looks like @"Sample 53 taken 21/3/1966.csv"

Your Category looked promising, as it took the fileName "programmatically" and not by selecting a real file's URL/Path, but it doesn't handle correctly such file-names.

By contrast, if I use NSSavePanel for saving a file, and I configure it with "suggested" file-name, I see later on, that the fileName appears to the user exactly as I suggested but the final URL and path I get from the NSSavePanel, have colons instead of slashes in the path --- and in the finder --- hooray! User sees slashes again in the file-name.

I don't know how to do this trick myself, and I find no method in the API for this. Could you enhance your NSURL category? or, hmmm, suggest a better solution to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment