Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, <code>stringByAddingPercentEscapesUsingEncoding</code> doesn't always work 100%. It encodes non-URL characters but leaves the reserved characters (like slash <code>/</code> and ampersand <code>&amp;</code>) alone. Apparently this is a <em>bug</em> that Apple is aware of, but since they have not fixed it yet, I have been using this category to url-encode a string:</p> <pre><code>@implementation NSString (NSString_Extended) - (NSString *)urlencode { NSMutableString *output = [NSMutableString string]; const unsigned char *source = (const unsigned char *)[self UTF8String]; int sourceLen = strlen((const char *)source); for (int i = 0; i &lt; sourceLen; ++i) { const unsigned char thisChar = source[i]; if (thisChar == ' '){ [output appendString:@"+"]; } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' || (thisChar &gt;= 'a' &amp;&amp; thisChar &lt;= 'z') || (thisChar &gt;= 'A' &amp;&amp; thisChar &lt;= 'Z') || (thisChar &gt;= '0' &amp;&amp; thisChar &lt;= '9')) { [output appendFormat:@"%c", thisChar]; } else { [output appendFormat:@"%%%02X", thisChar]; } } return output; } </code></pre> <p>Used like this:</p> <pre><code>NSString *urlEncodedString = [@"SOME_URL_GOES_HERE" urlencode]; // Or, with an already existing string: NSString *someUrlString = @"someURL"; NSString *encodedUrlStr = [someUrlString urlencode]; </code></pre> <hr> <p>This also works:</p> <pre><code>NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)unencodedString, NULL, (CFStringRef)@"!*'();:@&amp;=+$,/?%#[]", kCFStringEncodingUTF8 ); </code></pre> <hr> <p>Some good reading about the subject:</p> <p><a href="https://stackoverflow.com/q/3423545/836407">Objective-c iPhone percent encode a string?</a><br> <a href="https://stackoverflow.com/q/8086584/836407">Objective-C and Swift URL encoding</a> </p> <p><a href="http://cybersam.com/programming/proper-url-percent-encoding-in-ios" rel="noreferrer">http://cybersam.com/programming/proper-url-percent-encoding-in-ios</a><br> <a href="https://devforums.apple.com/message/15674#15674" rel="noreferrer">https://devforums.apple.com/message/15674#15674</a> <a href="http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/" rel="noreferrer">http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/</a> </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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