Scrungepipes
Scrungepipes

Reputation: 37581

How do you create an NSURL from a string containing spaces?

The result of this code is that url is null

NSString* home = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/"];
NSURL *url = [NSURL URLWithString:home];

and so is this:

NSString* home = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/"];
home = [home stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:home];

Upvotes: 3

Views: 9181

Answers (5)

tanmoy
tanmoy

Reputation: 1438

Swift 5.0 version:

let urlStrWithSpace = "path/test url"

guard let formattedUrl = urlStrWithSpace
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }

let sharePath = NSURL(string: formattedUrl)

Upvotes: 0

KorinW
KorinW

Reputation: 289

Swift 2.0 version:

let encodedPath = path?.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

Upvotes: 4

Jason Coco
Jason Coco

Reputation: 78343

First, if you're actually trying to get the application support directory for your application, use the appropriate method for the job (in this case, on NSFileManager) and work on the URL directly:

NSURL* appSupport = [[NSFileManager defaultManager] URLForDirectory: NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL];

If you really want to build a path, then use the appropriate initializer, in this case, tell the URL that it's a file path URL so that it will deal with spaces naturally and you can probably build up the URL path (this example is more like your code above):

NSString* home = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/"];
// Here, use the appropriate initializer for NSURL
NSURL *url = [NSURL fileURLWithPath:home];

Now, URL will be properly percent encoded and you won't have any problem (it will not be nil when returned).

Upvotes: 1

Steven Fisher
Steven Fisher

Reputation: 44876

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:

Upvotes: 12

CodaFi
CodaFi

Reputation: 43330

You actually need to add percent escapes, not remove them:

NSString* home = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Application Support/"];
    NSURL *url = [NSURL URLWithString:[home stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"%@", url);

which prints:

2012-07-18 13:44:54.738 Test[1456:907] /var/mobile/Applications/FF6E6881-1D1B-4B74-88DF-06A2B62CCFE6/Library/Application%20Support

Upvotes: 4

Related Questions