Make an NSURL with an encoded plus (%2B)

edoloughlin picture edoloughlin · May 22, 2009 · Viewed 9.7k times · Source

I need to pass a timestamp with a timezone offset in a GET request, e.g.,

2009-05-04T11:22:00+01:00

This looks like a two arguments "2009-05-04T11:22:00" and "01:00" to the receiving PHP script (over which I've no control).

NSURL doesn't encode plus signs, but if I make an NSURL using the string

2009-05-04T11:22:00%2B01:00

the url I end up with contains:

2009-05-04T11:22:00%252B01:00

Any ideas how I can preserve my encoded plus sign or just plain prevent NSURL from encoding anything?

Answer

anonymous picture anonymous · Feb 15, 2010

What worked for me was doing the UTF8 conversion, then replacing the + sign with %2B:

NSString *urlString =
    [NSString stringWithFormat:@"%@/iphone/push/create?pn[token]=%@&pn[send_at]=%@",
     kHTTPURL, appDelegate.deviceAPNToken, [dateTimeToUse description]];

urlString =
    [[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
     stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];