在网页中启动App

背景

最近有一个需求,在手机web网页上提供一个入口,指引用户打开 native app,或者下载。
该怎么作呢?css

Url Scheme

  • 这种方式下,被启动的 app 须要注册 Schema。
<!-- 关键所在,匹配相应的 scheme -->
<data android:scheme=”baidu” android:host=”test” />

打开的话,就是经过 URL, 它会匹配跳转到 app 对应的 activity 中
baidu://test/...html

  • 那么,URL 该如何抛出呢?
    若是经过 a.href ,若是没有安装应用,即打开失败,就会跳转到一个错误页面。
    由于没法获知调起 app,是否成功,也就没办法进行相应回调处理。

目前最理想的方式是经过 iframe.src,上代码:android

var openApp = {
    open: function(appurl, downurl) {
        var t = Date.now();

  var i = document.createElement('iframe');
  i.src = appurl,
  // i.style.cssText
  i.style.position = 'absolute',
  i.style.left = '-2000px',
  i.style.top = '-1000px',
  i.style.width = '1px',
  i.style.height = '1px',
  i.style.webkitTransition = 'all 0.9s',
  i.style.transition = 'all 0.9s',
  document.body.appendChild(i);
  var l = function () {
      document.body.removeChild(i);
if (Date.now() - t < 1500) {
    window.location.href = downurl;
      }
  };
  i.addEventListener('webkitTransitionEnd', l, false);
  i.addEventListener('transitionEnd', l, false);
  setTimeout(function () {
      i.style.left = '-1000px';
  }, 20);
    }
}

原理是:经过 schema 打开 app 以后,手机聚焦就不在 web网页 上了,进入后台,这样时间差就会变大。ios

可是这个方法,有两个问题:web

  • 在 Android Chrome 下没有效果,可是还好,它给出了本身的解决方案Android Intents with Chromechrome

  • 在 IOS9 及以上,没法直接调起,始终会到商店中去,不过也好,apple 给出方案Universal Link浏览器

Android Intents with Chrome

  • 代码:
intent:
  HOST/URI-path // Optional host
  #Intent;
      package=[string];
      action=[string];
      category=[string];
  end;

具体使用,还得根据自家产品来微信

  • 首先也须要在 app 中进行配置 apple-app-site-association 文件 :
{
  "applinks": {
    "apps": [],
    "details": [
    {
    "appID": "...",
    "paths": ["/open/*"]
    }
    ]
  }
}
  • 将 apple-app-site-association 文件上传到 web server.
  • 在 app 中处理通用连接 applinks:www.test1.com

这样第一次启动app的时候,会从 https://test1.com/apple-app-site-association 下载这个文件,交由 IOS 系统管理。这是,web网页能够直接发出请求 <a href="https://www.test2.com/open?xxx"> ,系统会作出拦截处理。app

参考 IOS9通用连接,这里讲得很详细了。
ide

PS:
微信中,除了自家产品,其余估计只有“在浏览器打开” 、“应用宝打开”。
UC 中,会拦截scheme,强加上 http(s)://

相关文章
相关标签/搜索