作者:吉翠芙_899 | 来源:互联网 | 2023-08-27 02:41
ImplementingCustomURLSchemesIfyourappcanreceivespeciallyformattedURLs,youshouldregisterth
1、Implementing Custom URL Schemes
If your app can receive specially formatted URLs, you should register the corresponding URL schemes with the
system. A custom URL scheme is a mechanism through which third-party apps can communicate with each
other. Apps often use custom URL schemes to vend services to other apps. For example, the Maps app supports
URLs for displaying specific map locations.
实现自定义的URL Schemes
如果你想让你的应用程序能够接收特定的、格式化的URLs,需要向系统中注册相应的URL schemes。自定义URL Scheme是一种机制,通过它,第三方应用能够相互通信。应用程序通常使用自定义的URL schemes为其它应用提供服务,例如,地图的应用提供显示指定位置的URLs。
2、Registering Custom URL Schemes
To register a URL type for your app, include the CFBundleURLTypes key in your app’s Info.plist file. The
CFBundleURLTypes key contains an array of dictionaries, each of which defines a URL scheme the app
supports. describes the keys and values to include in each dictionary.
注册自定义URL Schemes
注册URL type,需要向Info.plist文件中添加CFBundleURLTypes字段,CFBundleURLTypes对应一个数组,数组中的对象是字典,每个字典中定义了一个应用支持的URL scheme,下面列出了字典中的keys和values。
CFBundleURLName
A string containing the abstract name of the URL scheme. To ensure uniqueness, it is recommended that you specify a reverse-DNS style of identifier, for example, com.acme.myscheme. The
string you specify is also used as a key in your app’s InfoPlist.strings file. The value of the key is the human-readable scheme name.
CFBundleURLSchemes
An array of strings containing the URL scheme names—for example, http, mailto, tel, and sms.
CFBundleURLName 包含URL scheme的抽象名称,为了保证独一无二,推荐使用域名的倒序样式来定义,例如,com.acme.myscheme。同时可以在InfoPlist.strings文件中定义一个key为CFBundleURLName的键值对,其值是一个可读的scheme名。
CFBundleURLSchemes 是一个包含URL scheme名称的数组,例如,http,mailto,tel和sms。
3、Handling URL Requests
An app that has its own custom URL scheme must be able to handle URLs passed to it. All URLs are passed to
your app delegate, either at launch time or while your app is running or in the background. To handle incoming
URLs, your delegate should implement the following methods:
● Use the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods to retrieve information about the URL and decide whether you want to open it. If either method returns NO, your app’s URL handling code is not called.
● In iOS 4.2 and later, use the application:openURL:sourceApplication:annotation: method to open the file.
If your app is not running when a URL request arrives, it is launched and moved to the foreground so that it
can open the URL. The implementation of your application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method should retrieve the URL from its options
dictionary and determine whether the app can open it. If it can, return YES and let your
application:openURL:sourceApplication:annotation: (or application:handleOpenURL:) method handle the actual opening of the URL. (If you implement both methods, both must return YES before
the URL can be opened.) Figure 6-1 shows the modified launch sequence for an app that is asked to open a
URL.
处理URL请求
一个应用程序拥有自定义的URL scheme,就必须能够处理相应的URL请求,所有的URLs请求都会发送到应用程序的delegate,不论是在启动阶段还是应用在运行或者在后台,为了处理URL请求,delegate需要实现下面的方法:
●用application:willFinishLaunchingWithOptions:
或者 application:didFinishLaunchingWithOptions: 这两个方法获取到URL的信息,并且判断是否要打开此URL,只要两个方法中有一个返回NO,处理URL请求的代码就不会执行。
●在iOS4.2或者以后的版本中,使用application:openURL:sourceApplication:annotation:此方法来打开URL。
当一个URL请求时,应用程序未启动,启动并呈现这个可以打开的URL,这两个方法application:willFinishLaunchingWithOptions: 或者 application:didFinishLaunchingWithOptions: 接受到URL信息判断应用程序是否可以打开它,返回YES 就调用application:openURL:sourceApplication:annotation方法来打开URL,URL启动应用的流程如下:
如果应用已经在运行或者在后台时的执行流程如下:
以上是对apple官方文档中Custom URL Scheme的部分翻译,有翻译不对的或者理解错误的地方,还请各位大侠指正。我的微信公众账号是:DeveloperCave,有兴趣的同学可以加下,共同学习ios开发。另外,根据这个翻译写了一个小demo,可以点击此处下载。
Implementing Custom URL Schemes,布布扣,bubuko.com