Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c iPhone percent encode a string?

I would like to get the percent encoded string for these specific letters, how to do that in objective-c?

Reserved characters after percent-encoding !   *   '   (   )   ;   :   @   &   =   +   $   ,   /   ?   #   [   ] %21 %2A %27 %28 %29 %3B %3A %40 %26 %3D %2B %24 %2C %2F %3F %23 %5B %5D 

Percent-encoding wiki

Please test with this string and see if it do work:

myURL = @"someurl/somecontent" 

I would like the string to look like:

myEncodedURL = @"someurl%2Fsomecontent" 

I tried with the stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding already but it does not work, the result is still the same as the original string. Please advice.

like image 743
Hoang Pham Avatar asked Aug 06 '10 12:08

Hoang Pham


People also ask

How do I encode a percentage?

Percent-encoding is a mechanism to encode 8-bit characters that have specific meaning in the context of URLs. It is sometimes called URL encoding. The encoding consists of substitution: A '%' followed by the hexadecimal representation of the ASCII value of the replace character.

What is the best way to URL encode a string?

In JavaScript, PHP, and ASP there are functions that can be used to URL encode a string. PHP has the rawurlencode() function, and ASP has the Server.URLEncode() function. In JavaScript you can use the encodeURIComponent() function. Click the "URL Encode" button to see how the JavaScript function encodes the text.

How do I send a percentage in a URL?

To send a % sign in a url, instead send %25 . In your case, in order for php to see a percent sign, you must pass the character string %25B6011000995504101^SB to the server.

What is addingPercentEncoding?

addingPercentEncoding(withAllowedCharacters:)Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.


1 Answers

I've found that both stringByAddingPercentEscapesUsingEncoding: and CFURLCreateStringByAddingPercentEscapes() are inadequate. The NSString 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.

To fix this, I created an NSString category method to properly encode a string. It will percent encoding everything EXCEPT [a-zA-Z0-9.-_~] and will also encode spaces as + (according to this specification). It will also properly handle encoding unicode characters.

- (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 < sourceLen; ++i) {         const unsigned char thisChar = source[i];         if (thisChar == ' '){             [output appendString:@"+"];         } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||                     (thisChar >= 'a' && thisChar <= 'z') ||                    (thisChar >= 'A' && thisChar <= 'Z') ||                    (thisChar >= '0' && thisChar <= '9')) {             [output appendFormat:@"%c", thisChar];         } else {             [output appendFormat:@"%%%02X", thisChar];         }     }     return output; } 
like image 78
Dave DeLong Avatar answered Sep 19 '22 08:09

Dave DeLong