一。功能需求
公司推廣自己的活動,引導用戶打開本地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});
五。注意
此功能已在項目中使用,僅省略部分不重要代碼