iOS Universal Link通用链接

什么是Universal Link

Universal Link是苹果在WWDC 2015上提出的iOS 9 的新特新之一。此特性类似于deeplinking,能够方便地通过打开一个Https链接来启动手机app。这种新特性在实现web-app的无缝链接时能够提供极佳的用户体验。

Universal Link与URL Scheme对比

在iOS 9之前,基本都是通过Scheme方式,进行网页到应用的恢复过程。但Scheme无法判断手机是否安装App,而Universal Link在手机没有安装该应用的情况下可以跳到对应的https链接,而且通过Scheme打开App过程比较复杂,所以在iOS 9以后更推荐使用Universal Link
13640702-19acb986aaa7506b.png
Scheme打开App
13640702-20a08bc95e50edcf.png
Universal Link打开App

通过上面两张图对比,可以知道Universal Link能直接在微信打开App,更加便捷。

Universal Link的特性

  • 唯一性: 和自定义URL Scheme不一样,Universal Link不会被其他App使用,因为它使用标准的https链接到你自己的域名。
  • 安全性: 当你安装App时,iOS会检查你上传到web服务器的文件,确保你的网站允许App通过链接打开(双向认证)
  • 灵活性:Universal Link可以在手机没有安装App的情况下使用。当手机没有安装App使,可以通过Safari展示网站内容
  • 简易性:一个URL可以在项目与网站同时工作
  • 隐私性:其他App可以在不知道你App是否安装的情况与你的App通信

如何实现Universal Link

  • 在App中添加Associated Domains Entitlement
    13640702-b72a80b89bfec5ad.png
    在App中添加Associated Domains
    • webcredentials:webcredentials:example.com
      Use this service for shared web credentials.
      共享凭据:在可信的域名可以完成记住账号密码且自动填充
    • applinks:applinks:example.com
      Use this service for universal links.
      通用链接:通过可信的域名打开App
    • activitycontinuation:activitycontinuation:example.com
      Use this service for Handoff.
      Handoff:在同一个账号可以在不同的设备阅读相同的网址
  • 创建apple-app-site-association文件,并上传到服务器中
{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "TEAMIDSHSAUX.com.test.bundle",
                "paths": [ "*" ]
            }
        ]
    }
}

这是苹果官网上apple-app-site-association文件的示例,一个没有后缀的json格式文件
appID:TeamID.BundleID
paths:App支持的路径,注意paths对于大小写是敏感的

  • 在Appdelegate中进行处理
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
    NSURL *webUrl = userActivity.webpageURL;
    [self handleDeepLinkUrl:webUrl];
    return YES;
}

这个方法在web调起App的时候调用,把当前的URL传到该方法,App就更新到对应的页面

看到这里,应该对于Universal Link有了了解,按着上面的步骤应该也能实现web调起App的功能了
13640702-e19ca3461eed7bcf.gif
效果图


尊重原创,转载请注明出处,谢谢!

转载于:https://www.jianshu.com/p/e3b320c335f4

猜你喜欢

转载自blog.csdn.net/weixin_34151004/article/details/91145974
今日推荐