语言: swift, 版本:4.2,XCode:10.1
写作时间:2019-02-11

Scheme打开App

以前用Scheme方式打开app用类似于 taobao:// 这种方式,配置如下:

验证:在Safari输入链接:zscheme:// 就可以打开app

Alert提示host的内容, 在 AppDelegate.swift 添加如下代码:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {let message = url.host?.removingPercentEncodinglet alertController = UIAlertController(title: "Incoming Message", message: message, preferredStyle: .alert)let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)alertController.addAction(okAction)window?.rootViewController?.present(alertController, animated: true, completion: nil)return true}

浏览器访问链接 textreader://Hello!, 打开APP显示如下:

scheme打开App的弊端

  1. 任何App都可以用同一个名字,有些钓鱼App如果被安装了,可能被欺骗。
  2. 没有安装App的情况下,跳转到安装页面需要通过JavaScript去判断,也就是必须打开网页,做进一步跳转。

Universal Links说明

iOS 9 以后可以用Universal Links 打开App。当用户点击关联的host网页,无感地打开已经安装好的App(无需打开Safari)。如果没有安装App,就会打开链接到的网站。

Universal Links好处如下:

  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.

  2. Secure. 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.

  3. Flexible. 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.

  4. Simple. One URL works for both your website and your app.

  5. Private. Other apps can communicate with your app without needing to know whether your app is installed.

Demo工程下载

下载地址:
https://koenig-media.raywenderlich.com/uploads/2018/09/UniversalLinks.zip
UniversalLinks.xcodeproj 运行如下:

Universal Links实现步骤

1. App配置.

配置bundleId, 必须为付费Apple developer, 否则没法测试。

配置Associated Domains 为网站的host,网站必须为HTTPS协议。

applinks:universallinkszgpeace013001.herokuapp.com

路径为: UniversalLinks project > UniversalLinks target > Capabilities tab > Associated Domains.

2. 创建JSON文件apple-app-site-association

创建没有扩展名的JSON文件apple-app-site-association,格式如下,

{"applinks": {"apps": [],"details": [{"appID": "473VV338ZU.com.zgpeace.UniversalLinks","paths": [ "*"]},{"appID": "473VV338ZU.com.zgpeace.UniversalLinksNew","paths": [ "*"]}]}
}

applinks 表示app关联的网站信息。
apps 为空数据.
appID 由team ID和app’s bundle ID构成. (473VV338ZU.com.zgpeace.UniversalLinks 需要改为你的信息才能用)
点击链接查看team ID信息 > Membership:

paths 为app和网站可以关联的host,如果为通配符*号,则表示整个网站都可以链接到app,如果指定paths,没有在范围内的则不跳转到app。

3. 上传文件apple-app-site-association到HTTPS服务器

没有HTTPS服务器,这里可以部署到免费的服务器Heroku。
请fork Github地址:
https://github.com/zgpeace/universal-links
选择分支:final,

修改JSON配置文件apple-app-site-association的内容为你的信息,保存

编辑 README.md文件的部署链接为你fork下来的链接,注意是final分支,保存

最后,点击README.md 的部署按钮,部署到Heroku服务器即可。

笔者部署的链接为:https://universallinkszgpeace013001.herokuapp.com/

配置文件的内容可以通过链接访问:https://universallinkszgpeace013001.herokuapp.com/apple-app-site-association

{"applinks": {"apps": [],"details": [{"appID": "473VV338ZU.com.zgpeace.UniversalLinks","paths": [ "*"]},{"appID": "473VV338ZU.com.zgpeace.UniversalLinksNew","paths": [ "*"]}]}
}

4. APP接收跳转过来的事件

AppDelegate.swift 增加下面的方法1:

func presentDetailViewController(_ computer: Computer) {let storyboard = UIStoryboard(name: "Main", bundle: nil)guard let detailVC = storyboard.instantiateViewController(withIdentifier: "DetailController")as? ComputerDetailController,let navigationVC = storyboard.instantiateViewController(withIdentifier: "NavigationController")as? UINavigationController else { return }detailVC.item = computernavigationVC.modalPresentationStyle = .formSheetnavigationVC.pushViewController(detailVC, animated: true)
}

代码解析:上面方法处理子页面的展示,present model的方式。

AppDelegate.swift 增加下面的方法2:

func application(_ application: UIApplication,continue userActivity: NSUserActivity,restorationHandler: @escaping ([UIUserActivityRestoring]?
) -> Void) -> Bool {// 1guard userActivity.activityType == NSUserActivityTypeBrowsingWeb,let url = userActivity.webpageURL,let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {return false}// 2if let computer = ItemHandler.sharedInstance.items.filter({ $0.path == components.path}).first {presentDetailViewController(computer)return true}// 3if let webpageUrl = URL(string: "http://rw-universal-links-final.herokuapp.com") {application.open(webpageUrl)return false}return false
}

代码解析:

  1. 先校验userActivity是否为预期的字符. 最终你需要获取path的内容,跳转到相应的页面. 否则,你需要返回false,表示app不能处理该事件.

  2. 用path去查找对应的页面处理,present相应的detail View展示,返回true.

  3. 如果没有找到匹配的path,则用Safari打开URL.

验证

链接:https://universallinkszgpeace013001.herokuapp.com
因为url打开App需要跨域,验证可以把链接发送到邮箱,点击邮箱链接就可以打开。(如果把链接直接在Safari里面打开,是不会跳转的)

Limitations
Keep in mind that Safari will not attempt to launch the app if it is already on the same domain according to Apple’s documentation:
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.
You will need to either expose a url with a different domain (i.e. deeplink.example.com) or use a third-party service like branch.io, which supports custom domains.

实现了Universal Links, Safari打开网站,系统检测到APP已经安装,头部则会出现跳转的链接,点击链接即可打开。

注意:如果一个host的apple-app-site-association配置多个APP,则按照顺序遍历,遍历到则打开APP并退出遍历。

总结

恭喜你! 学会了Universal Links打开APP。

项目地址:
https://github.com/zgpeace/UniversalLinks

项目的starter 和 finished 各自实现了一个universal links,验证了apple-app-site-association配置2个APP的情况。

参考

https://www.raywenderlich.com/6080-universal-links-make-the-connection
https://www.appcoda.com/working-url-schemes-ios/
https://developer.apple.com/library/archive/documentation/General/Conceptual/AppSearch/UniversalLinks.html

拉起客户端 Universal Links Scheme Open App iOS(Deep Link)相关推荐

  1. 从客户端到服务器端,适配微信iOS OpenSDK中的Universal Links

    一.起始 微信iOS OpenSDK1.8.6版本开始,使用了iOS中的Universal Links.iOS 工程用pod一更新,使用到相关API的地方,全部报错,仔细一查看,API接口做了前面的改 ...

  2. 即学即用- URL Scheme、Universal Links

    首发地址 前言 在实际开发过程中,我们常会遇到微信分享.QQ分享.微博分享.微信支付.支付宝支付等诸如此类的需要唤醒第三方App的需求.但是在接入这些第三方SDK时,常见的两种手段,一种是使用URL ...

  3. Android App links 链接打开app功能

    1.深链接Deep link(URI SCHEME协议) 深链接即我们通常说的scheme跳转,需要我们在清单文件中对activity添加intent-fillter,并定义scheme(包括但不限于 ...

  4. iOS开发 APP拉起微信小程序Universal Links配置

    APP中有需要跳转微信小程序的需求,记录一下接入过程 步骤一:配置Associated Domains 1.登录苹果开发者中心,找到对应的Identifier勾选 Associated Domains ...

  5. IOS调起app的终极方法:Universal Links

    一.App"围墙" 各个移动App就像大海中的一座座岛屿,虽然都生活在一个海洋中(Android系统或iOS),但是他们之间通常是老死不相往来.举例来说,在微信应用中,用户基本上就 ...

  6. 浏览器中唤起native app || 跳转到应用商城下载(二) 之universal links

    上一篇文章 在ios9出来以后,我们发现越来越多的应用能够直接绕过微信的屏蔽,从其内置浏览器中直接唤起app.相比于通过弹窗提示让用户到浏览器中操作的方式,这无疑是极大的提高了用户体验与流量导入.因此 ...

  7. APP端ios接入微信支付分享 universal links 配置流程

    1.准备https的域名 Universal Links必须是https. 2.创建apple-app-site-association文件 创建apple-app-site-association文 ...

  8. iOS 微信打开第三方应用(微信跳转第三方app)(Universal Links)

    iOS应用可以使用 URL Schemes 进行应用间的跳转或实现网页打开应用的功能,这种跳转是协议跳转. 要应用间能跳转就要遵循URL Schemes协议. 要网页跳转应用网页就要遵循URL Sch ...

  9. Deep Link URL Scheme Universal Link

    Deep Link & URL Scheme & Universal Link Deep Link 在移动前端,deepLink一般指app内部的逻辑处理,可以根据这个deeplink ...

  10. iOS 9 通用链接(Universal Links)

    来源:iOS_小松哥 www.jianshu.com/p/734c3eff8feb 如有好文章投稿,请点击 → 这里了解详情 什么是Universal Links? 在iOS9之前,对于从各种从浏览器 ...

最新文章

  1. 下面关于html的描述正确的一项是,1 x 证书 Web 前端开发初级理论考试试卷五(2)...
  2. JAVAWEB入门之Servlet的注解配置
  3. java 获取ip地址_老杜带你学Java【第二课】
  4. 搜狗浏览器下 禁止浏览器自动填写用户名、密码
  5. IP分类以及子网掩码
  6. java设置http超时时间_Java设置Http请求超时时间
  7. java验证码图片工具类_工具类:VerifyCode.java:图片验证码
  8. 全网首发PHP版留言系统源码
  9. 光纤中传导模式matlab仿真,光纤通信实验指导书
  10. iOS从零开始,用Swift:iOS上的数据持久性和沙箱
  11. puppy linux u盘 分区,让安装在U盘上的Puppy Linux像安装在硬盘上一样工作
  12. 微信小程序新坑-文字溢出连续英文或者英文溢出 会出现字符
  13. 用我的计算器吧,我的python计算器贼牛逼
  14. ArangoDB——AQL编辑器
  15. 匈牙利算法python代码实现以及原理图解
  16. 音视频基础知识——素材理解
  17. 数据分析之np.random.choice()补充【从二维数组随机选择n行一维数组】
  18. 没有了剪辑和台本,papi酱为何要“强行”直播
  19. 如何享受人生,享受工作-读书笔记-得你所想、享你所得
  20. SpringBoot | 四大核心之actuator(程序监控器)

热门文章

  1. ASPNET2.0 发布问题
  2. 马什么梅?I什么N?浅谈 web 前端开发中的国际化
  3. 如何让jpa 持久化时不校验指定字段
  4. Laravel 跨域问题解决
  5. Python绘制分形树(一)
  6. REC Solar推出黑色多晶半切片PERC组件,已进入量产化
  7. HDU_1874_畅通工程续_最短路问题
  8. 工厂模式 接口 封装 实例
  9. java实现单链表的增加,删除,查找,打印
  10. 一位同学想通过用计算机编程解决韩信点兵,算法设计复习题