URL Encoding

  • I am trying to figure out how to properly URL Encode Urls in Objective-C
    Cocoa.
    I know this has been discussed before, and doing a quick search, I came
    across:

    CFURLCreateStringByAddingPercentEscapes

    with an example from here:

    http://www.cocoabuilder.com/archive/message/cocoa/2001/12/16/16098

    +(NSString *) urlencode: (NSString *) url

    {

    NSString* out = [(NSString*)

    CFURLCreateStringByAddingPercentEscapes(NULL,

    (CFStringRef) url,

    NULL,

    NULL, kCFStringEncodingUTF8)

    autorelease];

    return out;

    }

    I have tried multiple examples of this, but it does not encode my URLs.

    I ended up writing my own method to do this (which works):

    http://mesh.typepad.com/blog/2007/10/url-encoding-wi.html#more

    but I have this sneaking suspicion there is a better way.

    So, how should I url encode urls within Cocoa?

    thanks for any input / insight...

    mike chambers
  • On Oct 31, 2007, at 4:07 PM, Mike Chambers wrote:

    > So, how should I url encode urls within Cocoa?

    Do it again, but make sure you include all of the "tspecial"
    characters in URL strings, which are:

    ()<>@,;:\"/[]?=
    (note the space there at the end)

    Also, there's an encoding method that was added to NSString in Tiger
    which you might want to try...

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • On Oct 31, 2007, at 3:07 PM, Mike Chambers wrote:

    > I am trying to figure out how to properly URL Encode Urls in
    > Objective-C
    > Cocoa.
    > I know this has been discussed before, and doing a quick search, I
    > came
    > across:
    >
    > CFURLCreateStringByAddingPercentEscapes

    You're probably getting a little too low-level. NSString has -
    stringByAddingPercentEscapesUsingEncoding:, which is exactly what you
    want.

    -> jp
  • Thanks.  I had tried using
    stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding
    but was not having any luck.
    Here is my urlencode method:

    +(NSString *) urlencode: (NSString *) url

    {

    return [url
    stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

    }

    This is not encoding the url. I tried pretty much every encoding type
    (although i think ASCII is the correct one).

    mike chambers

    <mesh...>
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today