Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 122 lines (80 sloc) 4.671 kb

KSFileUtilities contains a variety of useful methods for handling URLs, paths and other aspects of file management. Here's a selection of them grouped by purpose:

Query Parameters

It's common for URLs to split their query string up into a dictionary-like series of parameters. e.g.

KSURLUtilities has an easy solution for getting that into a more Cocoa-friendly form:

- (NSDictionary *)ks_queryParameters;

There are also APIs for creating/deriving new URLs from a dictionary:

- (NSURL *)ks_URLWithQueryParameters:(NSDictionary *)parameters;

+ (NSURL *)ks_URLWithScheme:(NSString *)scheme
                       host:(NSString *)host
                       path:(NSString *)path
            queryParameters:(NSDictionary *)parameters;


An NSFormatter subclass for a variety of URL display and input tasks.

  • Non-complete URLs like example and generate full URL
  • Host URLs have a slash appended to them. e.g. is displayed/interpreted as
  • Convenience +URLFromString: method that takes care of unescaped characters

URL Normalization

The KSURLNormalization category implements normalization as documented by Wikipedia.

Relative Strings

NSURL is fully equipped to resolve relative strings against another URL, but sadly there's no API to go in the opposite direction. KSURLUtilities to the rescue!

- (NSString *)ks_stringRelativeToURL:(NSURL *)URL;
- (NSURL *)ks_URLRelativeToURL:(NSURL *)URL;

How's this work? Well, everyone loves examples right?

Receiver relativeToURL: Return Value foo/bar/ foo/example.png ../../ ../../baz/index.xml

Relative Paths

KSPathUtilities has an equivalent for operating on path strings: -ks_pathRelativeToDirectory:


To test if one URL/path contains another:

- [NSURL ks_isSubpathOfURL:]
- [NSString ks_isSubpathOfPath:]


Nothing fancy here, just a few additions to the built-in -[NSURL host] method.

- (NSURL *)ks_hostURL;

Strips a URL down to nothing but its scheme and host. e.g. becomes

- (NSArray *)ks_domains;

Splits the host up into its domains. e.g. gives (www, karelia, com)

URL Paths

Mac OS X 10.6 gave us a bunch of new URL methods like -URLByDeletingLastPathComponent. If you wanted access to this behaviour before, it necessitated much tedious mucking about with NSString path methods, or using CFURL functions. KSURLUtilities gives simple Cocoa APIs for all these tasks on 10.5 and earlier:

- (NSString *)ks_lastPathComponent;
- (NSString *)ks_pathExtension;
- (NSURL *)ks_URLByAppendingPathExtension:(NSString *)pathExtension;
- (NSURL *)ks_URLByDeletingLastPathComponent;
- (NSURL *)ks_URLByDeletingPathExtension;
- (BOOL)ks_hasDirectoryPath;
- (NSURL *)ks_URLByAppendingPathComponent:(NSString *)pathComponent isDirectory:(BOOL)isDirectory;

Incrementing a Path

On occasion there is a need to try to find the best available filename/path for a task. e.g. Choosing a filename that won't conflict with ay existing file. To simplify this, -[NSString ks_stringByIncrementingPath:] is offered. Given the path example.png, it will cycle through like so:


More information on the implementation and use of this method is on Mike's blog


A simple class that represents a URL, optionally with a title attached. Features:

  • Handles Web Location files (e.g. those created by dragging a URL from Safari)
  • NSCoding
  • NSCopying

You can pull in the WebKit.framework-dependent KSWebLocationPasteboardAdditions too, to (unsurprisingly) gain support for reading and writing Web Locations from/to the pasteboard.


Standard BSD. You know the drill.

Something went wrong with that request. Please try again.