Encoding a URL

Discussion in 'iPhone/iPad Programming' started by MarcVanH, Dec 12, 2008.

  1. macrumors newbie

    Joined:
    Sep 12, 2008
    #1
    I am having the toughest time encoding a URL that contains a query string.

    What I want to do is open a webbrowser control to a URL. I've done this several times before with no problems. Only difference this time - the URL has a query string on the end.

    http://my.host.com/path?address=123 Main Street

    Now, of course, "123 Main Street" needs to be encoded first. This is where I am having the trouble.

    The following example code illustrates my problem:

    Code:
    NSString *myUrl = [@"123 main St" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSLog(myUrl);
    I would expect the output to be "123%20main%20St". But instead, the output is garbage.

    Can anyone tell me why this is? Is this a bug?
     
  2. macrumors regular

    Joined:
    Mar 24, 2008
    #2
    try adding retain to it.

    or alloc init the string, then do the encoding.
     
  3. thread starter macrumors newbie

    Joined:
    Sep 12, 2008
    #3
    Thanks for the suggestions - but unfortunately neither worked. Still garbage out.
     
  4. macrumors newbie

    Joined:
    Aug 30, 2007
    #4
    Your URL encoding is working just fine...it's your call to NSLog that's causing you pain.

    Try this instead:

    Code:
    NSLog(@"%@", myUrl);
     
  5. macrumors regular

    Joined:
    Mar 24, 2008
    #5
    oh yeah... i remember the pain of me forgetting to put the @ symbol back in the day.
     
  6. macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #6
    Side note on this - you can't "alloc init the string, then do the encoding." You can not change a string at all once it is created - you can create new strings, but you can't change an existing one.
     
  7. thread starter macrumors newbie

    Joined:
    Sep 12, 2008
    #7
    Thanks very much. I feel a little silly.


     

Share This Page