I have been teaching myself Objective-C and Cocoa (in part so I can build native iPhone / iTouch apps), and have been working on a simple command line application named "turl". This basically provides a command-line interface to url shortening services such as tinyurl.com and urltea.com.
Anyways, I ran into an issue where a specific URL was not being created correctly. It turns out, it was because I was not url encoding the url before sending it to the api. No problem I thought, I'll just url encode it. However, after much searching, I found that Cocoa didn't really provide a url encode API. I did find find a couple of possible solutions that used other APIs, but I could not get either one to work for me, and it didn't appear that they were made specifically to URL encode urls.
So, I finally decided to make my own and figured I would post it here in case anyone else needs to use such an API.
//simple API that encodes reserved characters according to:
//RFC 3986
//http://tools.ietf.org/html/rfc3986
+(NSString *) urlencode: (NSString *) url
{
NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" ,
@"@" , @"&" , @"=" , @"+" ,
@"$" , @"," , @"[" , @"]",
@"#", @"!", @"'", @"(",
@")", @"*", nil];
NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F" , @"%3F" ,
@"%3A" , @"%40" , @"%26" ,
@"%3D" , @"%2B" , @"%24" ,
@"%2C" , @"%5B" , @"%5D",
@"%23", @"%21", @"%27",
@"%28", @"%29", @"%2A", nil];
int len = [escapeChars count];
NSMutableString *temp = [url mutableCopy];
int i;
for(i = 0; i < len; i++)
{
[temp replaceOccurrencesOfString: [escapeChars objectAtIndex:i]
withString:[replaceChars objectAtIndex:i]
options:NSLiteralSearch
range:NSMakeRange(0, [temp length])];
}
NSString *out = [NSString stringWithString: temp];
return out;
}
Basically, it encode a string according to RFC 3986.
As I mentioned above, I am just learning Objective-C and Cocoa, so there may be a better way to do this, and the code above may need some tweaks. If you have any suggestions, please post them in the comments.
I just barely got into shortwave and have been searching the web for help. I am very glad to say that all you have posted has been so very helpful. Especially when I saw you were having the exact same problems as I was. Anyway I hope you start posting again and maybe you might be able to answer a few question through the email. I think shortwave is really cool and love how so few people know about it. btw I have an eton grundig g2000. They do not make this model anymore I think. In a few weeks the grundig g6aviator will be out and I plant to buy it. Thanks again for info you have posted. laters.....
Posted by: Christopher (Mr_Skulls) | March 06, 2008 at 10:24 AM
Have you tried this?
NSString* escapedUrlString = [unescapedString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
Posted by: Shane Harvie | April 05, 2008 at 05:47 PM
Thanks for sharing - this was useful, though you may want to add space to %20 mapping.
Posted by: rakoth | April 17, 2008 at 06:37 AM
I found this code sample on another site. Seems to do the job.
+(NSString *) urlEncode: (NSString *) url
{
NSString *result=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return result;
}
Posted by: Jeremy | August 25, 2008 at 02:58 PM
Works like a charm. I added support for spaces, since URL Encoding should also turn a @" " into a @"+". Other than that, I tried all the other encoding strategies discussed (using the stringByAdding... functions), but they never worked quite right.
Thanks!
Chris
Posted by: Chris | September 15, 2008 at 10:29 AM
You should also release temp. That mutable copy needs a release or it leaks.
Or, you could avoid the copy to the "out" string and just return [temp autorelease];
Posted by: Dave | October 30, 2008 at 07:23 PM
Hey,
I'm a .NET developer and recently I've taken a few looks at Objective-C... but boy... it's simply scary....
Apple is great, but not for developers...
Forcing us to use Objective-C to get things done....
Anyway, what resources did you use to learn it...
Posted by: TimothyP | December 03, 2008 at 08:41 AM
@TimothP
I posted the resources I found most useful here:
http://mesh.typepad.com/blog/2008/12/resources-for-learning-cocoa.html
mike chambers
[email protected]
Posted by: mesh | December 03, 2008 at 09:21 AM
Hi,
In fact, you don't really need to do that by your own.
NSURL *theURL = [[NSURL alloc] initWithScheme:@"http"
host:@"www.wopata.com"
path:@"/do?q=foo bar"];
Will return the following URL: http://www.wopata.com/do?q=foo%20bar
Posted by: Jean-Etienne | December 04, 2008 at 06:48 AM