views:

23

answers:

2

Hi there I have this code

NSLog(@"%@",URLRequestQueryString);
NSString *sendToServerString = [NSString stringWithFormat:@"http://mydomain.co.uk/req.php%@",URLRequestQueryString];
NSURL *sendToServer = [[NSURL alloc] initWithString:sendToServerString];
NSLog(@"%@",sendToServer);
NSLog(@"%@",sendToServerString);

URLRequestQueryString is just a standard querystring that I have built up throughout the script.

The first NSLog works fine and outputs a proper querystring (if I copy and paste it into a browser then the page will load and run correctly.

This is also the case when I output sendToServerString it correctly outputs the URL with querystring (which I can also copy and paste into a browser).

However sendToServer ouputs (null). If I remove the querystring it will correctly output the domain and path.

Any idea why this happens? How can I sort it?

Thank you.

+1  A: 

NSURL *sendToServer = [NSURL URLWithString: [sendToServerString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
Could be what you are looking for. :)

Gubb
That worked thank you. :) What does that do anyway? ps: will accept as an answer in a bit - it won't let me for another 4 mins.
Thomas Clayson
No worries! Can't say I'm sure of what it does, but I think it allocates an autoreleased-instance of NSURL and adds percent-escapes (such as %20 instead of "space") where needed.
Gubb
ah I see... brilliant - basically like URL Encoding? :) Thanks a lot. :) have some rep.
Thomas Clayson
+1  A: 

You need see NSString reference. A Section named "Working with URLs". Under this section has two method

Toro
Thank you. :) What does replacingParentEscapes do? Does that reverse the process?
Thomas Clayson
Yes, it will remove the percent escape encoding.
Toro