h5打开应用市场
方法一:实测
// 判断当前是安卓设备还是ios设备
export function getDeviceType() {
const userAgent = navigator.userAgent;
if (/Android/i.test(userAgent)) {
return 'Android';
} else if (/iPhone|iPad|iPod/i.test(userAgent)) {
return 'iOS';
} else {
return 'Other';
}
}
//是否为微信环境
export function isWeChatEnvironment() {
const userAgent = navigator.userAgent;
return /MicroMessenger/i.test(userAgent);
}
//页面逻辑
const handleDowloadApp = () => {
const type = getDeviceType();
if (type === 'iOS') {
window.location.href = 'itms-appss://apps.apple.com/cn/app/id6504797302';
} else if (type === 'Android') {
detectDeviceBrand();
} else {
console.log('其他');
}
};
const detectDeviceBrand = () => {
/* 包名 xxxxxxx*/
if (isWeChatEnvironment()) {
window.location.replace('https://a.app.qq.com/o/simple.jsp?pkgname=xxxxxxx');
return;
}
window.location.replace('market://details?id=xxxxxxx');
};
方法二:未测试
const CONFIG = {
iosScheme: 'itms-appss://apps.apple.com/cn/app/id6504797302', // 替换成你的 iOS 应用 URL
iosDownload: 'itms-appss://apps.apple.com/cn/app/id6504797302', // 替换成你的 iOS 下载页面 URL
androidScheme: 'com.example.app://',
androidDownload: 'https://play.google.com/store/apps/details?id=com.example.app', // 替换成你的 Android 下载页面 URL
};
export const openApp = () => {
const userAgent = navigator.userAgent;
const iframe: HTMLIFrameElement = document.createElement('iframe');
iframe.style.display = 'none'; // 确保 iframe 不会显示在页面上
const startTime = new Date().getTime();
let hasApp = true; // 假设设备上已安装应用
console.log(hasApp);
if (/iPhone|iPod|iPad/i.test(userAgent)) {
iframe.src = CONFIG.iosScheme; // 尝试打开 iOS 应用的 URL
setTimeout(() => {
const endTime = new Date().getTime();
if (endTime - startTime < 2000) {
hasApp = false;
window.location.href = CONFIG.iosDownload; // 跳转到 iOS 下载页面的 URL
}
document.body.removeChild(iframe);
}, 1000);
} else if (/android/i.test(userAgent)) {
iframe.src = CONFIG.androidScheme; // 尝试打开 Android 应用的 URL
setTimeout(() => {
const endTime = new Date().getTime();
if (endTime - startTime < 2000) {
hasApp = false;
window.location.href = CONFIG.androidDownload; // 跳转到 Android 下载页面的 URL
}
document.body.removeChild(iframe);
}, 1000);
}
};