Launching Your Own Application via a Custom URL Scheme
http://mobiledevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
Posted on December 14, 2008 by Rodney Aiglstorfer in Cocoa
Before you get started, you need to figure out how you want you application to respond to the URL. The simplest way to use custom schemes is to just “wake up”; but it is also possible to pass information to the application via the URL, and in so doing, enable the application to do different things when woken up.
Registering Custom URL Schemes
Regardless of what you want to do once your application is started, the first step is to register a custom URL scheme with the iPhone. This is done via theinfo.plist
file located in your application’s project folder (NOTE: this is the same file you would change to define a custom icon).By default, when opened, XCode will edit the file in a graphical UI. It is possible to edit the info.plist file directly in Text mode which may be easier for some people.
Step 1. Right-Click and “Add Row”
CFBundleURLTypes CFBundleURLSchemes myapp CFBundleURLName com.yourcompany.myapp
Optionally Handle the URL
Now that the URL has been registered. Anyone can start the application by opening a URL using your scheme.Here are a few examples …
myapp:// myapp://some/path/here myapp://?foo=1&bar=2 myapp://some/path/here?foo=1&bar=2
If you want to provide a custom handler, simply provide an implementation for the message in your delegate. For example:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { // Do something with the url here }
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if (!url) { return NO; } NSString *URLString = [url absoluteString]; [[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"]; [[NSUserDefaults standardUserDefaults] synchronize]; return YES; }
Related posts:
No comments:
Post a Comment