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

HarryWorksInc

macrumors regular
Original poster
Feb 21, 2010
179
0
I am currently working on an application that contacts a mySQL data base using php. In my app there is a string that will be submitted to the server but obviously if the string has a space in it the app cannot load the URL. Does anyone know how to replace spaces in a URL to %20?
 
Ok, sorry i've been looking over the NSStringEncoding and if I am understanding the Class References correctly there is no string encoder for a URL or for replacing " " with "%20". Can you give an example or just the encoder?
 
Ok, sorry i've been looking over the NSStringEncoding and if I am understanding the Class References correctly there is no string encoder for a URL or for replacing " " with "%20". Can you give an example or just the encoder?

See the post two above this one: there is not an encoding (as this is not a character encoding in that sense of the word). But there is a method to escape those characters.
 
One last thing I,v been incorporated the code
HTML:
[URL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
but it doesn't seem to be working. When i return the url the spaces are still spaces.
 
One last thing I,v been incorporated the code
HTML:
[URL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
but it doesn't seem to be working. When i return the url the spaces are still spaces.

Post your code.

Anyway I can guess what you are doing:

Code:
NSString *URL = @"my url with spaces";
[URL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"%@",URL); // Logs my url with spaces

But, as we all know, NSString is immutable. And the method returns an NSString. So the obvious usage is:
Code:
NSString *URL = @"my url with spaces";
URL = [URL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"%@",URL); // Logs my%20url%20with%20spaces
 
Oh, thanks I was still using an NSMutableString but once i changed it back to an NSString and it worked.
 
Oh, thanks I was still using an NSMutableString but once i changed it back to an NSString and it worked.

I can see how this could be confusing but using a NSMutableString does not alter the signature or semantics of the method. If NSMutableString provided a method that did the same thing in-place it would not return a NSString object...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.