b2cloud

27th May 2012

iOS URL Schemes

Guides | Tutorial By 5 years ago

If you need your iOS apps to talk to each other, one of the best ways to do this is using a custom URL scheme. Your app can register a URL scheme that will cause the OS to launch your application whenever that URL is used. A good example of this is the Facebook URL scheme, open this blog post on your iPhone/iPad and click fb:test. Mobile Safari will launch the native Facebook application (provided you have it on your device).

Apps using URL schemes are not just limited to being launched, you can also pass information to the target application. Anything in the urlĀ can be read from the application, useful for things such as displaying a specific address in the Google Maps app.

To show these in action I will walk through making two applications, one will send two numbers to the other application which will add them together and send back the result. This will also demonstrate switching back to the original application after the result is determined.

Download the project start point. There are two projects in here, MainApp and AdderApp. MainApp will call AdderApp which will do the calculations and give the result back to MainApp.

Open both projects, first we need to specify which custom URLs each app will use. In the AdderApp’s info plist add the following.

In this, b2adder is our URL scheme, meaning any url along the lines of “b2adder:” will launch this application.

Add the following in the MainApp’s info plist.

Now that both have a URL scheme it will be possible for AdderApp to launch back into MainApp after the result is calculated.

Now for some real code, in MainApp’s AppDelegate find the addClicked: method. In here add the following code

- (IBAction) addClicked:(id)sender
{
	if(number1.text.length <= 0 || number2.text.length <= 0)
		return;

	NSLog(@"Adding: %@ + %@", number1.text, number2.text);

	NSString* selfUrlScheme = [[[[[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleURLTypes"] objectAtIndex:0] valueForKey:@"CFBundleURLSchemes"] objectAtIndex:0];

	NSURL* schemeURL = [NSURL URLWithString:[NSString stringWithFormat:@"b2adder:%@,%@,%@", number1.text, number2.text, selfUrlScheme]];

	[[UIApplication sharedApplication] openURL:schemeURL];
}

This piece of code will create a url to open AdderApp. In the url it specifies the two numbers and our own URL scheme so AdderApp knows which app to switch back to. We could hard code our own URL scheme but you can use the code above to grab it dynamically from the info plist, providing it's in the correct order. I recommend using GET variables and pulling out the values that way, but for this example I'm separating each value with commas.

Now in AdderApp's AppDelegate go to the application:openURL:sourceApplication:annotation: method. Add the following code

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
	NSArray* components = [[[url.absoluteString componentsSeparatedByString:@":"] objectAtIndex:1] componentsSeparatedByString:@","];

	NSInteger number1 = [[components objectAtIndex:0] integerValue];
	NSInteger number2 = [[components objectAtIndex:1] integerValue];
	NSString* callerUrlScheme = [components objectAtIndex:2];

	NSInteger result = number1 + number2;

	NSURL* schemeURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@:%i", callerUrlScheme, result]];

	[[UIApplication sharedApplication] openURL:schemeURL];

	return YES;
}

This just pulls out the two numbers and the caller app's URL scheme, adds them together and launches the caller app with the result.

Now back to MainApp's application:openURL:sourceApplication:annotation:, simply show an alert view with the result.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
	NSString* result = [[url.absoluteString componentsSeparatedByString:@":"] objectAtIndex:1];

	[[[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ + %@ =", number1.text, number2.text]
								 message:result
								delegate:nil
					   cancelButtonTitle:@"OK"
					   otherButtonTitles:nil] autorelease] show];

	return YES;
}

Put both apps on your iPhone/iPad or iOS Simulator and then launch MainApp. It will do as suggested.

You can find the project end point here.

Recommended Posts

App processing vs server processing

Post by 5 years ago

When building an app that connects to a web service it’s often best to put the processing load on your server, as opposed to the app. For example, say your web service returned a list

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!