Flutter实现从网页唤醒App

623 阅读2分钟

Android端

在AndroidManifest.xml文件中配置,就可以实现在html中通过a标签打开App实现唤醒App

<a href="icheerapp://app.emulator.cn">打开App</a>

配置如下:

<activity
    ...
    <!-- App Links
    在网页中通过<a href="icheerapp://app.emulator.cn">打开App</a>实现唤醒App
    -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="icheerapp"
            android:host="app.emulator.cn" />
    </intent-filter>
</activity>

iOS端

在iOS端中,有两种方式可以实现:

  1. URLScheme方式通过配置Info.plist里的CFBundleURLTypes
  2. Associated Domains配置 Universal Link

URLScheme方式

Info.plist添加配置,在浏览器中可通过“icheerapp://”即可唤醒App

<key>CFBundleURLTypes</key>
<array>
	<dict>
		<key>CFBundleTypeRole</key>
		<string>Editor</string>
		<key>CFBundleURLName</key>
		<string>icheerapp</string>
		<key>CFBundleURLSchemes</key>
		<array>
			<string>icheerapp</string>
		</array>
	</dict>
</array>

Universal Link方式

universal link 是IOS 9之后推出的一种能够方便地通过https的链接的形式来启动App的方法

相比较于 scheme 跳转 universal link的优势在哪里

  • 唯一性 scheme 由于是自定义的方式,所以存在不同的App定义同一个scheme的场景,在同时安装的场景下,会发生跳转互串。而universal link 本身基于https协议进行跳转,所用域名具备唯一性,一般不会发生两个App用同一个universal link的场景。
  • 安全 当用户下载app时,会去配置的config网址下载universal link的配置,这个后台的根目录其它人是没有权限进行更改的。目前一些分享SDK已经渐渐半强制使用universal link,主要也是为了安全性。
  • 功能更全面 app 未安装时 scheme 是无效的,但是universal link可以去走一个落地页,引导用户去安装app。https的方式使用起来也比scheme的方式更便捷

开始配置universal link

1、需要一台具备https的服务器,能够将文件上传到根目录下 (尝试过阿里云oss,无后缀的格式不支持,所以没调试通)   

2、准备一个json的配置文件 apple-app-site-association 上传到服务器上的根目录下

{ "applinks": { "apps": [], "details": [ { "appID": "9JA89QQLNQ.com.apple.wwdc", "paths": [ "/wwdc/news/", "/videos/wwdc/2015/"] }, { "appID": "ABCD1234.com.apple.wwdc", "paths": [ "" ] } ] }}

3、苹果开发者中心 打开 Associated Domains 模块,更新对应证书,工程上添加对应的 domains 通用链接地址