h5打开应用市场

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);
	}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值