Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Tuesday, July 30, 2013

iOS URL Encoding

So, you want to go from NSString to URL, huh?

Occasionally, you come across this dirty bit of iOS, where you want to encode a string to create a URL, so you try something like [mystring stringByAddingPercentEscapesUsingEncoding]... and while this works SOME of the time, it doesn't work all of the time, because it won't escape ALL of the necessary special characters to make this work.

So, here's a quick and dirty implementation that I use with great effect:

First, I define this slick little method here (don't be scared of the old Objective-C syntax and low-level libraries, embrace it!):

-(NSString *)urlenc:(NSString *)val
    CFStringRef safeString =
    return [NSString stringWithFormat:@"%@", safeString];

... and I use it like so:

NSString *query =
    [NSString stringWithFormat:@"",
     [self urlenc:@"some(people+-?/{}"],
     [self urlenc:@"just#want@to!watch<the>world,.burn"]];
NSURL *url = [NSURL URLWithString:query];

Which yields:

Works like a charm.

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates