1.点击下面官网详解英文版—苹果官网Universal Links详解
When you support universal links, iOS 9 users can tap a link to your website and get seamlessly redirected to your installed app without going through Safari. If your app isn’t installed, tapping a link to your website opens your website in Safari.
如果你的app支持通用链接,那么iOS 9用户点击链接到你的网站,可以准确地定位到到手机安装的你的APP应用程序,而不通过Safari去实现。如果用户手机没有安装你的APP应用程序,通过通用连接能利用一个链接到你的网站,而不用通过在Safari打开你的网站。
Universal links give you several key benefits that you don’t get when you use custom URL schemes. Specifically, universal links are:
通用链接给你如下几个关键的好处,让你不必使用自定义 URL schemes来实现(这种实现方式已经被微信给屏蔽了,所以说通过自定义链接协议头的方式在微信里加载网页,点击网页跳转到APP,已经不可能了)。具体来说,好处是:
1.Unique.唯一性
Unlike custom URL schemes, universal links can’t be claimed by other apps, because they use standard HTTP or HTTPS links to your website.
不像自定义URL schemes协议头那样,通用的链接不能被其他应用程序声称,因为他们使用标准HTTP或HTTPS链接你的网站。
2.安全
When users install your app, iOS checks a file that you’ve uploaded to your web server to make sure that your website allows your app to open URLs on its behalf. Only you can create and upload this file, so the association of your website with your app is secure.
如果iOS用户安装了你的应用,应用程序启动(不是登陆)时就会检查你上传到web服务器json文件(apple-app-site-association),以确保你的网站允许应用程序打开url。只有你能创建和上传这个文件,所以专门配置的网站与你的应用程序连接之间是安全的。
3.方便,自由性
Universal links work even when your app is not installed. When your app isn’t installed, tapping a link to your website opens the content in Safari, as users expect.
触发通用连接时,如果用户没有安装你的应用程序,点击这个通用连接会打开Safari加载你的网站内容,这是用户所期望。
4.简单
One URL works for both your website and your app.
只需一个URL就能根据用户有没有安装你的APP,决定加载你的网站还是你的APP应用程序
5.私有性。
Other apps can communicate with your app without needing to know whether your app is installed.
其他应用程序可以与你的应用程序通信,而不需要知道你的应用程序是否在用户手机中安装。
NOTE
In iOS 9 and later, universal links let users open your app when they tap links to your website within WKWebView and UIWebView views and Safari pages, in addition to links that result in a call to openURL:, such as those that occur in Mail, Messages, and other apps.
When a user is browsing your website in Safari and they tap a universal link to a URL in the same domain as the current webpage, iOS respects the user’s most likely intent and opens the link in Safari. If the user taps a universal link to a URL in a different domain, iOS opens the link in your app.
For users who are running versions of iOS earlier than 9.0, tapping a universal link to your website opens the link in Safari.
iOS中9,后来,用通用链接让用户打开你的应用时,点击链接到你的网站内,可以使用WKWebView和UIWebView视图和Safari网页来实现,除了调用openURL链接:比如那些发生在邮件、消息和其他应用程序,这些途径是不能实现的。
用户在Safari浏览你的网站,可以利用这个通用链接实现一个相同的域名URL来加载当前网页,iOS尊重用户的最有可能的意图和使用Safari打开链接。如果用户通用链接到一个在图通域名的URL,iOS将在你的应用程序打开链接(这个功能类似于知乎APP)。
iOS的用户正在运行的iOS系统版本比9.0低,网页出发的通用链接会在Safari浏览器打开你的网站链接,而不会触发打开你的APP(也就是手机系统必须是iOS9.0以上)。
Adding support for universal links is easy. There are three steps you need to take:
1.Create an apple-app-site-association file that contains JSON data about the URLs that your app can handle.
2.Upload the apple-app-site-association file to your HTTPS web server. You can place the file at the root of your server or in the .well-known subdirectory.
3.Prepare your app to handle universal links.
You can test universal links on a device.
添加支持通用链接是很容易的。你需要有三个步骤:
Creating and Uploading the Association File
To create a secure connection between your website and your app, you establish a trust relationship between them. You establish this relationship in two parts:
创建你的网站和应用程序之间的安全连接,你需要建立二者的信任关系。建立这种关系的两个部分:
1. An apple-app-site-association file that you add to your website
2. A com.apple.developer.associated-domains entitlement that you add to your app (this part is described in Preparing Your App to Handle Universal Links)
You can learn more about how your app and website can share credentials in Shared Web Credentials Reference.
苹果官网共享网页凭证连接
你可以学习更多关于你的应用和网站可以在共享网络共享凭证凭证参考。
If your app runs in iOS 9 or later and you use HTTPS to serve the apple-app-site-association file, you can create a plain text file that uses the application/json MIME type and you don’t need to sign it. If you support Handoff and Shared Web Credentials in iOS 8, you still need to sign the file as described in Shared Web Credentials Reference.
如果你的应用程序运行在iOS 9或更高版本和使用HTTPS apple-app-site-association文件,您可以创建一个纯文本文件使用application / json的MIME类型,你不需要签字。如果你在iOS 8中支持传递和共享网络凭据,你仍然需要签署的文件共享网络中描述参考凭证。(不晓得…..)
You need to supply a separate apple-app-site-association file for each domain with unique content that your app supports. For example, apple.com and developer.apple.com need separate apple-app-site-association files, because these domains serve different content. In contrast, apple.com and www.apple.com don’t need separate site association files—because both domains serve the same content—but both domains must make the file available. For apps that run in iOS 9.3.1 and later, the uncompressed size of the apple-app-site-association file must be no greater than 128 KB, regardless of whether the file is signed.
你需要为每个域名(不同的内容)提供一个单独的apple-app-site-association文件,以便你的应用支持不同跳转。例如,apple.com和developer.apple.com需要不同的单独的apple-app-site-association文件,因为这些域名会提供不同的内容。相比之下,apple.com和www.apple.com网站不需要独立的配置文件,因为两个域名提供相同的内容,但是这两个必须都提供的配置文件。应用程序运行在iOSiOS 9.3.1及以后得系统,未压缩的apple-app-site-association文件的大小不能大于128 KB,无论文件是否签署。
In your apple-app-site-association file, you specify the paths from your website that should be handled as universal links along with those that should not be handled as universal links. Keep the list of paths fairly short and rely on wildcard matching to match larger sets of paths. Listing 6-1 shows an example of an apple-app-site-association file that identifies three paths that should be handled as universal links.
apple-app-site-association文件中,你从你的网站,应该指定路径哪些应该处理为通用链接以及那些不应该处理为通用链接。保持相当短的路径列表和依靠通配符匹配匹配更大的路径集。清单6 - 1显示了一个示例的一个apple-app-site-association文件,确定三个路径应该处理通用链接。
Listing 6-1Creating an apple-app-site-association file
{
"applinks": {
"apps": [],
"details": [
{
"appID": "9JA89QQLNQ.com.apple.wwdc",
"paths": [ "/wwdc/news/", "/videos/wwdc/2015/*"]
},
{
"appID": "ABCD1234.com.apple.wwdc",
"paths": [ "*" ]
}
]
}
}
Don’t append .json to the apple-app-site-association filename.
apple-app-site-association文件名,不要带.json后缀
The apps key in an apple-app-site-association file must be present and its value must be an empty array, as shown in Listing 6-1. The value of the details key is an array of dictionaries, one dictionary per app that your website supports. The order of the dictionaries in the array determines the order the system follows when looking for a match, so you can specify an app to handle a particular part of your website.
apple-app-site-association中的apps key必须存在,它的值必须是一个空数组,如清单6 - 1所示。details key的值是一个包含字典的数组,你的网站所支持跳转的一个应用程序对应一个字典。数组中的字典的顺序决定了系统寻找对应匹配的顺序,所以您可以指定一个应用程序来处理一个特定的网站的一部分。
Each app-specific dictionary contains an appID key and a paths key. The value of the appID key is the team ID or app ID prefix, followed by the bundle ID. (The appID value is the same value that’s associated with the “application-identifier” key in your app’s entitlements after you build it.) The value of the paths key is an array of strings that specify the parts of your website that are supported by the app and the parts of your website that you don’t want to associate with the app. To specify an area that should not be handled as a universal link, add “NOT ” (including a space after the T) to the beginning of the path string. For example, the apple-app-site-association file shown in Listing 6-1 could prevent the /videos/wwdc/2010/* area of the website from being handled as a universal link by updating the paths array as shown here:
每个app-specific字典包含一个appIDkey和paths key。
appID key是团队ID的值或应用程序ID前缀,紧随其后的是boundle ID。(appID key 就是在你的应用程序的“application-identifier”)
paths key是一个字符串数组,指定你的网站的哪些部分支持跳转到应用程序和哪些网站的部分不想跳转到应用。指定一个域名,不应该作为一个通用的链接处理,在域名paths字符路径的前部添加“NOT”(包括空间T)。例如,apple-app-site-association文件显示在清单6 - 1–/videos/wwdc/2010/*区域被处理为一个通用的网站链接通过更新paths数组如下所示:(翻译的有点绕,不好意思哈)
"paths": [ "/wwdc/news/", "NOT /videos/wwdc/2010/*", "/videos/wwdc/201?/*"]
Because the system evaluates each path in the paths array in the order it is specified—and stops evaluating when a positive or negative match is found—you should specify high priority paths before low priority paths. Note that only the path component of the URL is used for comparison. Other components, such as the query string or fragment identifier, are ignored.
因为系统通过paths数组里的路径顺序来评估每个路径的顺序,当积极或消极匹配被发现时,系统就可以指定和停止评估,所以你应该指定路径的高低优先级。注意,只有路径的URL的部分用于比较。其他部分,如查询字符串或片段标识符,都将将被忽略。
There are various ways to specify website paths in the apple-app-site-association file. For example, you can:
有多种方法来指定网站apple-app-site-association文件中的路径。例如,您可以:
1. Use * to specify your entire website
2. Include a specific URL, such as /wwdc/news/, to specify a particular link
3. Append * to a specific URL, such as /videos/wwdc/2015/*, to specify a section of your website
4. In addition to using * to match any substring, you can also use ? to match any single character. You can combine both wildcards in a single path, such as /foo/*/bar/201?/mypage.
1. 使用*指定您的整个网站
2. 包括特定的URL,比如/wwdc/news/,指定一个特定的链接
3. 添加*到特定的URL,比如/videos/wwdc/2015/*,匹配你的网站的指定部分
4. 除了使用*匹配任意子串,您还可以使用?匹配任何单个的字符。您可以组合两个通配符在一个路径,如/foo/*/bar/201?/mypage
NOTE
The strings you use to specify website paths in the paths array are case sensitive.
请注意
你使用的字符串来指定网站路径的路径数组是大小写敏感的。
After you create the apple-app-site-association file, upload it to the root of your HTTPS web server or to the .well-known subdirectory. The file needs to be accessible via HTTPS—without any redirects—at https:///apple-app-site-association or https:///.well-known/apple-app-site-association. Next, you need to handle universal links in your app.
在您创建apple-app-site-association文件后,上传到你的HTTPS web服务器跟目录或者.well-known的子目录。文件需要通过HTTPS访问-没有任何重定向-at https:// / apple-app-site-association或https:// / .well-known / apple-app-site-association。接下来,您需要在你的应用程序处理通用链接。//
## 准备APP来处理通用连接 ##
Universal links use two technologies: The first is the same mechanism that powers Handoff between a web browser and a native app, and the second is Shared Web Credentials (for more information about these technologies, see Web Browser–to–Native App Handoff and Shared Web Credentials Reference). When a user taps a universal link, iOS launches your app and sends it an NSUserActivity object that you can query to find out how your app was launched.
通用链接使用两种技术:
第一个是相同的机制之间的web浏览器和本地应用的跳转功能交接,
第二是共享网络凭据(关于这些技术的更多信息,请参见web Browser-to-Native应用传递和共享网络凭据引用)。当用户利用一个通用链接,iOS启动应用程序并将其发送一个NSUserActivity对象,您可以查询找出应用程序是如何启动的。
To support universal links in your app, take the following steps:
支持通用链接在你的应用程序,采取以下步骤:
1. Add an entitlement that specifies the domains your app supports.
2. Update your app delegate to respond appropriately when it receives the NSUserActivity object.
1. 添加一个entitlement以便应用程序支持的指定的域名.
2. 更新您的应用程序delegate,当它接收NSUserActivity对象作出适当反应。
In your com.apple.developer.associated-domains entitlement, include a list of the domains that your app wants to handle as universal links. To do this in Xcode, open the Associated Domains section in the Capabilities tab and add an entry for each domain that your app supports, prefixed with applinks:, such as applinks:www.mywebsite.com. Limit this list to no more than about 20 to 30 domains.
在你com.apple.developer.associated-domains entitlement,包括域名列表,应用程序要处理的通用链接。在Xcode中,打开相关领域部分的功能选项卡,并添加一个条目,您的应用程序支持的每个域,与applinks:前缀,如applinks:www.mywebsite.com。这个列表限制不超过20到30域名。
To match all subdomains of an associated domain, you can specify a wildcard by prefixing *. before the beginning of a specific domain (the period is required). Domain matching is based on the longest substring in the applinks entries. For example, if you specify the entries applinks:*.mywebsite.com and applinks:*.users.mywebsite.com, matching for the domain emily.users.mywebsite.com is performed against the longer *.users.mywebsite.com entry. Note that an entry for *.mywebsite.com does not match mywebsite.com because of the period after the asterisk. To enable matching for both *.mywebsite.com and mywebsite.com, you need to provide a separate applinks entry for each.
配所有子域的相关域名,可以通过加前缀指定一个通配符。在一个特定的域的开始(period是必需的)。域名匹配是基于最长的子串applinks条目。applinks:例如,如果您指定条目 .mywebsite.com和applinks:.users.mywebsite.com 匹配的域名emily.users.mywebsite.com对长进行.users.mywebsite.com条目。注意* .mywebsite.com不匹配的条目 mywebsite.com,因为星号后的period.为了匹配.mywebsite.com和mywebsite.com,您需要分别提供单独的applinks条目。
After you specify your associated domains, adopt the UIApplicationDelegate methods for Handoff (specifically application:continueUserActivity:restorationHandler:) so that your app can receive a link and handle it appropriately.
在您指定相关域名后,采用UIApplicationDelegate的方法(application:continueUserActivity:restorationHandler:),这样你的应用可以收到一个链接并适当地处理它。
When iOS launches your app after a user taps a universal link, you receive an NSUserActivity object with an activityType value of NSUserActivityTypeBrowsingWeb. The activity object’s webpageURL property contains the URL that the user is accessing. The webpage URL property always contains an HTTP or HTTPS URL, and you can use NSURLComponents APIs to manipulate the components of the URL.
当用户点击通用链接后iOS系统启动应用程序时,APP会收到一个NSUserActivity对象,这个对象的activityType 类型是NSUserActivityTypeBrowsingWeb类型。这个对象的webpageURL属性包含用户访问的URL。webpage的URL属性总是包含一个HTTP或HTTPS URL,并且可以使用NSURLComponents api来操纵这部分URL。
When a user taps a universal link that you handle, iOS also examines the user’s recent choices to determine whether to open your app or your website. For example, a user who has tapped a universal link to open your app can later choose to open your website in Safari by tapping a breadcrumb button in the status bar. After the user makes this choice, iOS continues to open your website in Safari until the user chooses to open your app by tapping OPEN in the Smart App Banner on the webpage.
当用户点击了你操控通用链接时,iOS还检测用户最近的选择来决定是否打开你的应用程序或网站。例如,利用一个通用连接的用户打开应用程序后可以选择在状态栏点击导航按钮,在Safari打开链接你的网站连接。iOS用户作出选择后,继续在苹果Safari浏览器上打开你的网站,直到用户选择在网页上的the Smart App Banner点击打开应用程序。
NOTE
If you instantiate a SFSafariViewController, WKWebView, or UIWebView object to handle a universal link, iOS opens your website in Safari instead of opening your app. However, if the user taps a universal link from within an embedded SFSafariViewController, WKWebView, or UIWebView object, iOS opens your app.
请注意
如果你实例化一个SFSafariViewController、WKWebView或UIWebView对象处理通用链接,iOS在Safari打开你的网站,而不是打开你的应用。但是,如果用户点击的通用链接是嵌入在SFSafariViewController WKWebView,或UIWebView对象中,iOS系统会打开你的应用。
It’s important to understand that if your app uses openURL: to open a universal link to your website, the link always opens in Safari instead of getting redirected to your app. iOS recognizes that the call originates from your app and therefore should not be handled as a universal link.
如果你的应用程序使用openURL:打开一个通用链接到你的网站,通用连接总是你尝试在Safari中打开网页链接,而不是去唤醒你的APP实现跳转,iOS系统识别到调用源自你的应用,因此不应该作为一个通用的链接处理。对这个过程的理解是很重要的.(感觉就是如果是加载普通的网页链接,就不要用通用连接处理)
If you receive an invalid URL in an activity object, it’s important to fail gracefully. To handle an unsupported URL, you can call openURL: on the shared application object to open the link in Safari. If you can’t make this call, display an error message to the user that explains what went wrong.
如果你从一个活动对象接收到一个无效的URL,友好的失败提示是很重要的。处理一个不受支持的URL,你可以叫openURL:共享应用程序对象在Safari中打开链接。如果你不能使这个调用,向用户显示一条错误消息,解释出错原因。
IMPORTANT
To protect users’ privacy and security, you should not use HTTP when you need to transport data; instead, use a secure transport protocol such as HTTPS.
重要的
为了保护用户的隐私和安全,不应该使用HTTP当你需要传输数据;相反,使用一个安全的传输协议,比如HTTPS。
说明:翻译的还不是完全准确,等具体功能实现了,在细细修订细节