Encode a URL in iOS with a Utility Choreo


Temboo does lots more than just work with APIs. We've got a growing collection of programming utilities that will make your life much easier. In this tutorial we'll show you how to use these handy code shortcuts by working through a URL encoding example.

You might ask "why is URL encoding important?". Well, URLs can only be sent over the Internet using the ASCII character-set. Since URLs often contain non-ASCII characters, they need to be converted so they only contain valid characters. The general rule is that unsafe characters are replaced with a % followed by two hexadecimal digits. Spaces are replaced by either a plus sign (+) or with %20. You've probably seen URLs full of strange looking characters, and now you know why!

The good news is that you don't actually have to worry about the specifics. Our URL encoding utility will do all the work for you.

Get Set Up

1 Log in to Temboo. If you don't already have an account, you can register for free.

2Make sure you've downloaded the Temboo iOS SDK and that you've added it to your development environment as described in our iOS getting started tutorial.

Test the Choreo

3Go to the Utilities > Encoding > URLEncode Choreo directly from our website. Select iOS from the drop down menu at the top of the page.

4Enter some text that you want to URL encode. It should contain illegal characters, like spaces, accents, and punctuation. Here's some text you can use:

I'm Enc0ding URL$ wITh T3mBo0

5 Click Generate Code to test the Choreo from our website, and you'll see the the URL encoded result returned by the Choreo.

Create Your iOS Program

6 Create a new OS X > Application > Command Line Tool project in Xcode. Make sure to add the core directory from the Temboo iOS SDK and the TMBUtilities.h and TMBUtilities.m files as described in our getting started tutorial.

7 Copy the code below into your main.m class, between the Foundation.h import statement and the main method.

#import "TMBUtilities.h"
#import "TMBChoreography.h"
#import "TMBTembooSession.h"

@interface URLEncode : NSObject <TMBChoreographyDelegate>
-(void)runURLEncodeChoreo;
-(void)choreographyDidFailWithError:(NSError*)error;
-(void)choreographyDidFinishExecuting:(TMBUtilities_Encoding_URLEncode_ResultSet*)result;
@end

@implementation URLEncode

-(void)runURLEncodeChoreo {
	// Instantiate the Choreo, using a previously instantiated TembooSession object, eg:
	TMBTembooSession *session = [[TMBTembooSession alloc] initWithAccount:@"ACCOUNT_NAME" appKeyName:@"APP_NAME" andAppKeyValue:@"APP_KEY"];
	
	// Create the choreo object using your Temboo session
	TMBUtilities_Encoding_URLEncode *uRLEncodeChoreo = [[TMBUtilities_Encoding_URLEncode alloc] initWithSession:session];	
	
	// Get Inputs object for the choreo
	TMBUtilities_Encoding_URLEncode_Inputs *uRLEncodeInputs = [uRLEncodeChoreo newInputSet];

	// Set inputs
	[uRLEncodeInputs setText:@"I'm Enc0ding URL$ wITh T3mBo0"];

	// Execute choreo specifying this class as the choreo delegate
	[uRLEncodeChoreo executeWithInputs:uRLEncodeInputs delegate:self];
}

// TMBChoreographyDelegate method implementation - handle choreo errors
-(void)choreographyDidFailWithError:(NSError*)error {
	// Log error to the console
	NSLog(@"Error - %@", error);
}

// TMBChoreographyDelegate method implementation - choreo executed successfully
-(void)choreographyDidFinishExecuting:(TMBUtilities_Encoding_URLEncode_ResultSet*)result {
	// Log results to the console
	NSLog(@"%@", [result getURLEncodedText]);
}

@end

8 Update your main method to run the Choreo:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        URLEncode *test = [[URLEncode alloc] init];
        [test runURLEncodeChoreo];
        [[NSRunLoop currentRunLoop] run];
    }
    return 0;
}

9Run the code and the URL encoded version of the text will be printed in the console.

What next?

We're all finished! Now you know about Temboo's range of handy developer utilities and how to access them via our iOS SDK. Check out the rest of the 2000+ Choreos in our Library and see how you can combine them with our utilities to build something amazing.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back