Launching Your Own Application via a Custom URL Scheme
来源:互联网 发布:java程序员专业技能 编辑:程序博客网 时间:2024/06/07 06:52
One of the coolest features of the iPhone SDK is an application’s ability to “bind” itself to a custom URL scheme and for that scheme to be used to launch itself from either a browser or from another application on the iPhone. Creating this kind of binding is so simple, its almost criminal not to use it in your application!
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”
Step 2. Select “URL types” as the Key
Step 3. Expand “Item 1″ and provide a value for the URL identifier. This can be any value, but the convention is to use a “reverse domain name” (ex “com.myapp”).
Step 4. Add another row, this time to “Item 1″.
Step 5. Select “URL Schemes” as the Key.
Step 6. Enter the characters that will become your URL scheme (e.g. “myapp://” would be “myapp”). It is possible for more than one scheme to be registered by adding to this section though that would be strange thing to do.
NOTE: If you open the info.plist file in a text editor you will see the following has been added to the file …
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
The iPhone SDK, when launching the application in response to any of the URLs above, will send a message to theUIApplicationDelegate.
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}
A common technique is to parse the URL passed in and pull from it the parameters that will be used by various views in the application and store them in the User Preference. Below is an example where we store the URL as a value parameter “url” in just such a manner …
- (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;}
Now you have everything you need to enable others to wake-up your application and even pass it information! Enjoy!
From: http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
See also: http://wiki.akosma.com/IPhone_URL_Schemes#Portscan
- Launching Your Own Application via a Custom URL Scheme
- Launching Your Own Application via a Custom URL Scheme
- Launching Your Own Application via a Custom URL Scheme
- Launching Your Own Application via a Custom URL Scheme(在短信链接中打开你的程序并且给你的程序发送消息)
- Launching Your iPhone App Via Custom URL Scheme
- 44. Building Your Own Application
- How to write your own custom Form
- Implement your own short url
- iOS:开放平台引用(一)--应用外打开app / respond to a custom URL scheme
- Develop your own Magento mobile application
- 简单的Mac-Custom-URL-Scheme-demo
- Linux Commands for Beginners--Create Your Own Commands Via alias
- 7 Steps to Write Your Own Custom Rule using FXCOP
- Tailor Your Application by Building a Custom Forms Designer with .NET
- Tailor Your Application by Building a Custom Forms Designer with .NET
- How to add a Custom Pull to Refresh in your iOS Application
- Why your ASP.NET MVC Application needs its own framework
- 通过custom url scheme启动应用程序 下载、评价
- 用jxl读写excel的.xls文件的例子 带样式
- 关于Label的背景色透明
- asp读取数据库教程
- 通达工作流在开发管理中的应用
- Js日期操作一
- Launching Your Own Application via a Custom URL Scheme
- 重构代码的7个阶段
- Js日期操作二
- 四种方法恢复最近关闭的Chrome标签页
- FDN的使用
- Crashlytics告诉你iOS应用为何崩溃
- C++之<<每天一点点>>
- javascript动态操作表格:新增、设置样式、删除、移动行
- 新手建站必知的七条SEO避忌知识