Your question is not about creating an URL from a string containing spaces, but from a path string containing spaces.
For a path, you should not use URLWithString
. Mac OS X and iOS include convenience functions for building NSURLs for file paths that handle this and more for you automatically. Use one of them instead.
Apple's documentation for [NSURL fileURLWithPath: path]
says:
This method assumes that path is a directory if it ends with a slash. If path does not end with a slash, the method examines the file system to determine if path is a file or a directory. If path exists in the file system and is a directory, the method appends a trailing slash. If path does not exist in the file system, the method assumes that it represents a file and does not append a trailing slash.
As an alternative, consider using fileURLWithPath:isDirectory:
, which allows you to explicitly specify whether the returned NSURL
object represents a file or directory.
Also, you should be using NSSearchPathForDirectoriesInDomains
to find the Application Support directory.
Putting this all together, you'd end up with something like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath: applicationSupportDirectory isDirectory: YES];
Source: