转载注明出处:点击打开链接
H5无法调起android app 的坑之 scheme 大小写
项目中遇到的坑,此处记录一下,也为大家提个醒。
1. 在manifest 文件中配置h5打开activity的scheme和host
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@style/AppThemeNoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="host"
android:scheme="myApp" /> <!--此处有坑,勿效仿,请阅读下文-->
</intent-filter>
</activity>
2. 在html中的简单调用
<!Doctype html>
<html>
<head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<title> my test </title>
</head>
<body>
<a href="myApp://main">click me to jumpping to the main page </a>
</body>
</html>
此处h5 的调用方式确认无误,可参考。
3. 遇到的问题
html无论如何也调不起app,排查了各种原因,仔细核对了 scheme 和 host 都和h5
的一样,却就是调不起来。
4. 解决方式
4.1 转机
震惊!我无意中看到了这样一幕,彻底让我茅塞顿开..
看到没有,震不震惊?html里明明写的是 myApp,到浏览器里却变成了 myapp,也就是说,scheme在浏览器里是不分大小写的,会统一转为小写。
4.2 解决问题
震惊!我无意中看到了这样一幕,彻底让我茅塞顿开..
于是,我在android的manifest中配置filter时,把scheme改为小写,测试之后欣然发现 bug解了,app调起来了~
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="host"
android:scheme="myapp" /> <!--此处需要用全小写-->
</intent-filter>
4.3 注意
后来因为好奇,测试了一下,不仅仅是scheme,host 也会被转为小写,所以在manifest中配置时,scheme 和 host 都要全为小写。