js 通过schema协议拉起本地app

一。功能需求

  公司推广自己的活动,引导用户打开本地app或下载app,并获取浏览器等数据传给服务端做统计用

二。实现流程

  1.在实例化的同时,获取设备数据给服务器做统计用,再打开应用的同时native会记录下相应数据,这样可以和h5的数据进行比对,确定是否是同一个用户的操作。(width,height,channelId渠道好,type系统类型,version系统版本号),并获取一个记录android手机通过schema拉起本地app会报错或其他异常的接口数据

  2.在用户调用wakeuporinstall方法时,开始尝试拉起本地app和下载应用,

    2.1 判断本地设备系统类型

    2.2 若是ios 直接跳转到app store,因为当本地没有安装应用的情况下,在尝试拉起本地应用时会报网址无效的错误

    2.3 若是android 会拿到之前获取的通过schema拉起本地app报错或异常的接口数据和本地获取的数据进行对比,如果比对成功,则直接提示下载,否则拉起本地app和下载应用同时进行(下载应用会timeout:1000)

三。功能实现

 
 
function OpenInstall(para) {

 
 
    this.schema = {
iphoneSchema: "com.test.schema://",
androidSchema: "app://com.test.schema.mainapp",
};
this.downloadUrl = {
downUrl: "http://test.com/download.html?version=" + this.marketVersion,
iphoneDownUrl: "https://itunes.apple.com/us/app/test/id1111111111?l=zh&ls=1 &mt=8",
androidDownUrl: "https://test.testd.com/test-" + this.marketVersion + "-relea se.apk",
};
    //请求数据
 
 
    var paras = {
chanelId: this.channel,
width: screen.width,
height: screen.height,
type: this.getOS().type,
version: this.getOS().version
};
 
 
    this.queryInfo(paras);
    this.queryNoneSupportBrowsers();
 
 
 
 
 
 
    this.wakeupOrInstall = function (para) {
this.openApp(para.timeout);
};
    this.openApp = function (timeout) {
timeout = timeout || 1000;
var osType = this.getOS().type;//获取系统类型
var startTime = Date.now();
if (osType === 'iOS') {
window.location.href = this.downloadUrl.iphoneDownUrl;
}
else {//android
if (this.isToDownload()) {//根据当前数据和服务器数据的比对判断是否直接下载
window.location.href = this.downloadUrl.androidDownUrl;
} else {
window.location.href = this.schema.androidSchema;
var t = setTimeout(this.toDownload.bind(this, startTime, osType, timeout), timeout);
}
}
};
    this.toDownload = function (startTime, osType, timeout) {
var endTime = Date.now();
if (!startTime || endTime - startTime < timeout + 200) {
window.location.href = this.downloadUrl.androidDownUrl;
}
};
}
四。使用
var openInstall = new OpenInstall();
openInstall.wakeupOrInstall({timeout: 1000});

五。注意
此功能已在项目中使用,仅省略部分不重要代码

 

转载于:https://www.cnblogs.com/wangyuanyuan-blog/p/6860907.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值