Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately you need to be smarter than what is provided by Apple :</p> <pre><code>stringByAddingPercentEscapesUsingEncoding: </code></pre> <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> <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> <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> <p>Here is the code:</p> <pre><code>@interface NSURL (SmartEncoding) + (NSURL *)smartURLWithString:(NSString *)str; @end @implementation NSURL (SmartEncoding) + (NSURL *)smartURLWithString:(NSString *)str { CFStringRef preprocessed = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)str, CFSTR(""), kCFStringEncodingUTF8); if (!preprocessed) preprocessed = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)str, CFSTR(""), kCFStringEncodingASCII); if (!preprocessed) return [NSURL URLWithString:str]; CFStringRef sanitized = CFURLCreateStringByAddingPercentEscapes(NULL, preprocessed, NULL, NULL, kCFStringEncodingUTF8); CFRelease(preprocessed); NSURL *result = (NSURL*)CFURLCreateWithString(NULL, sanitized, NULL); CFRelease(sanitized); return [result autorelease]; } @end </code></pre> <p>It works fine with UTF8 string encoded and ASCII ones.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload