Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MarcVanH

macrumors newbie
Original poster
Sep 12, 2008
15
0
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?
 

MarcVanH

macrumors newbie
Original poster
Sep 12, 2008
15
0
Thanks for the suggestions - but unfortunately neither worked. Still garbage out.
 

Scratch75

macrumors newbie
Aug 30, 2007
10
0
Your URL encoding is working just fine...it's your call to NSLog that's causing you pain.

Try this instead:

Code:
NSLog(@"%@", myUrl);
 

xsmasher

macrumors regular
Jul 18, 2008
140
0
try adding retain to it.

or alloc init the string, then do the encoding.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.