objective-cnsstringescapingsignpercent-encoding

Objective-C URL string contains single percent sign


I have a string which ends with percent sign(%),

this string is prepared for an URL request as a parameter:

NSString *parameter = @"/param=%";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL urlWithString:[NSString stringWithFormat:@"http://www.whatev%@",parameter]]];

The request returns nil.

I've tried:

NSString *parameter = @"/param=\uFF05";
//request returns nil

and

NSString *parameter =  @"/param=%";
NSString *newParameter = [parameter stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//request returns /param=%25 ...where does 25 come from?!

How could I have only one % converted to a request url?

Any advice would be appreciated.


Solution

  • The percent sign has a special purpose in URL's and is used to encode special characters of all kinds. For example a space ( ) is %20 and the percent sign itself is %25.

    http://en.wikipedia.org/wiki/Percent-encoding

    The last one should be correct, I assume you have problems using it as a request?