简体   繁体   中英

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];

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:

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

Swift 2.0版本:

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM