Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've found that both <code>stringByAddingPercentEscapesUsingEncoding:</code> and <code>CFURLCreateStringByAddingPercentEscapes()</code> are inadequate. The <code>NSString</code> method misses quite a few characters, and the CF function only lets you say which (specific) characters you want to escape. The proper specification is to escape all characters except a small set.</p> <p>To fix this, I created an <code>NSString</code> category method to properly encode a string. It will percent encoding everything EXCEPT <code>[a-zA-Z0-9.-_~]</code> and will also encode spaces as <code>+</code> (according to <a href="http://tools.ietf.org/html/draft-hammer-oauth-10#section-3.6" rel="noreferrer">this specification</a>). It will also properly handle encoding unicode characters.</p> <pre><code>- (NSString *) URLEncodedString_ch { 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>
    singulars
    1. This table or related slice is empty.
    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