实现从浏览器自动打开深层链接有不同的策略......不幸的是,没有一种方法可以在任何地方都适用......
最常见的一种是尝试导航到深层链接并在片刻后回退到应用商店:
<script>
// when the page is loaded...
window.onload = function() {
// and viewed on an Android Phone...
if (navigator.userAgent.match(/Android/)) {
// then try to open the deep link
window.location.replace('myapp://foo');
// if it didn't work after half a second, then redirect the
// user to the app store where he can download the app
setTimeout(function() {
window.location.replace('http://play.google.com/store/apps/details?id=com.myapp');
}, 500);
}
}
</script>
这适用于 Android 旧版浏览器和大多数 webviews...
对于 Chrome,您必须使用 intent:// 网址。要使其正常工作,您必须在直接用户输入后导航到那里:这意味着您不能尝试在window.onload 回调中自动加载intent:// url。实现它的最简单方法是从您的服务器重定向到intent:// url:
# This example assumes a Ruby on Rails app
def show
if request.user_agent.match /Android/
redirect_to 'intent://...'
else
render # render your website normally
end
end
如需更深入的信息,请阅读 Google Chrome 工程师的这篇博文:Deep App Linking on Android and Chrome。
如果您不想自己处理所有这些问题,那么您可以使用提供深度链接的 3rd-party 服务,例如branch.io 或 Shortcut Media(免责声明:我目前在 Shortcut Media 工作)。