<p>In an application that I'm writing I have some code like this:</p>

<pre><code>NSWorkspace* ws = [NSWorkspace sharedWorkspace];
NSString* myurl = @"http://www.somewebsite.com/method?a=%d";

NSURL* url = [NSURL URLWithString:myurl];

[ws openURL:url];
</code></pre>

<p>The main difference being that <em>myurl</em> comes from somewhere outside my control. Note the %d in the URL which isn't entirely correct and means that URLWithString fails, returning <em>nil</em>.</p>

<p>What is the "correct" way of handling this? Do I need to parse the string and properly encode the arguments? Or is there some clever method in Cocoa that does all the hard work for me?</p>

[{'answer_id': 45905, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 2, 'selected': False, 'text': "<p>I think the behaviour here is correct, because %d is not a valid component of a URL (% is the escape, but expects two hex characters to follow it).</p>\n\n<p>You can't just URL encode the URL as given to you, because that would encode the /s and ?s as well, which you don't want.</p>\n\n<p>So, the question is, what's the correct behaviour here?</p>\n\n<p>Perhaps you would want it to be turned into...</p>\n\n<pre><code>http://www.somewebsite.com/method?a=%25d\n</code></pre>\n\n<p>(i.e. the % is encode to the encoded version of % in a URL, so when method gets the input, it sees a as being set to %d)</p>\n\n<p>I don't think there's any library function which will do that sort of thing for you, since there's no 'correct' way to do it. About he only correct thing you can do is return an error message saying the URL you were given is invalid (just as URLWithString is)</p>\n\n<hr>\n\n<p>If you wanted to try to handle the input, I guess you would need to search the URL for any % symbols which are not immediately followed by two hex characters, and then replace the % with %25 in that case. That should be quite possible with a regular expression, though I suspect there may be some additional complexities if your URLs start containing encoded versions of characters outside the ASCII character set.</p>\n"}, {'answer_id': 46212, 'author': 'amrox', 'author_id': 4468, 'author_profile': 'https://Stackoverflow.com/users/4468', 'pm_score': 4, 'selected': True, 'text': '<p>I\'m not sure if this is exactly what you\'re looking for, but there is a method in NSString that will sanitize a URL:</p>\n\n<p><a href="https://developer.apple.com/documentation/foundation/nsstring/1415058-stringbyaddingpercentescapesusin" rel="nofollow noreferrer">stringByAddingPercentEscapesUsingEncoding:</a></p>\n'}, {'answer_id': 8665933, 'author': 'nverinaud', 'author_id': 855846, 'author_profile': 'https://Stackoverflow.com/users/855846', 'pm_score': 1, 'selected': False, 'text': '<p>Unfortunately you need to be smarter than what is provided by Apple :</p>\n\n<pre><code>stringByAddingPercentEscapesUsingEncoding:\n</code></pre>\n\n<p>This will escape all invalid URL characters so that "http://foo.com/hey%20dude/", which is valid, becomes "http://foo.com/hey%2520dud/", which is not what we want. </p>\n\n<p>According to apple documentation : <a href="http://developer.apple.com/library/mac/documentation/CoreFOundation/Reference/CFURLRef/Reference/reference.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes" rel="nofollow">http://developer.apple.com/library/mac/documentation/CoreFOundation/Reference/CFURLRef/Reference/reference.html#//apple_ref/c/func/CFURLCreateStringByAddingPercentEscapes</a></p>\n\n<p>I made an NSURL category which does the right thing and works with odd string such as ones with partial encoding (i.e. "http://foo.com/hey dude/i%20do%20it/").</p>\n\n<p>Here is the code:</p>\n\n<pre><code>@interface NSURL (SmartEncoding)\n+ (NSURL *)smartURLWithString:(NSString *)str;\n@end\n\n@implementation NSURL (SmartEncoding)\n\n+ (NSURL *)smartURLWithString:(NSString *)str\n{\n    CFStringRef preprocessed = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)str, CFSTR(""), kCFStringEncodingUTF8);\n    if (!preprocessed) \n        preprocessed = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)str, CFSTR(""), kCFStringEncodingASCII);\n\n    if (!preprocessed)\n        return [NSURL URLWithString:str];\n\n    CFStringRef sanitized = CFURLCreateStringByAddingPercentEscapes(NULL, preprocessed, NULL, NULL, kCFStringEncodingUTF8);\n    CFRelease(preprocessed);\n    NSURL *result = (NSURL*)CFURLCreateWithString(NULL, sanitized, NULL);\n    CFRelease(sanitized);\n    return [result autorelease];\n}\n\n@end\n</code></pre>\n\n<p>It works fine with UTF8 string encoded and ASCII ones.</p>\n'}]
['https://Stackoverflow.com/questions/45898', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2998/']