uniapp h5在浏览器唤起app

本文详细介绍了如何实现在H5页面中点击打开APP的功能,针对已安装和未安装APP的用户进行不同处理。在Android和iOS平台上,通过配置manifest.json文件来设置scheme。对于微信环境,由于特殊性,需要额外处理并跳转到应用市场。此外,还提供了一段JavaScript代码示例,用于监听页面可见性变化,以判断和执行相应的唤醒或跳转操作。

需求

当用户打开h5 链接时候 ,点击打开app, 若用户在已经安装过app的情况下直接打开app,若未安装过跳到应用市场下载安装

这个功能在实现上主要分为两种场景,从普通浏览器唤醒以及从微信唤醒。这两个场景又分为了IOS端和安卓端

配置

安卓
在manifest.json的"app-plus"->“distribute”->"android"节点下添加schemes数据:“schemes” : “nameAPP”

"schemes" :  ["nameAPP"]

在这里插入图片描述

IOS
在manifest.json的"app-plus"->“distribute”->"ios"节点下添加urltypes数据:

"urltypes": [{
                "urlidentifier": "xxx.xxx", //  一般为域名倒写,例如 taobao.com
                "urlschemes": ["nameAPP"]
   }],
   "urlschemewhitelist":["nameAPP"]	 //白名单

在这里插入图片描述

注意

下面代码的 nameAPP:// 就是代表着schemes/Universal Link 通用链接,也就是跳到自己app 的快捷链接 如果链接需要带参数的话redxiang://params ,这里的我写的是没带参数的,我只做了微信浏览器的判断,因为微信直接打开的h5链接是不能直接换起app的,需要在微信开放平台配置相关参数**(偷个懒直接让它跳到应用市场)。目前直接获取当前手机是android还是ios

微信开放标签文档

<view class="sign-box" @click="myplayUser">
				立即下载
</view>
data() {
			return {
				timer:null,
				opening:null,
			}
		},

这里做了监听visibilitychange事件是浏览器新添加的一个事件,当浏览器的某个标签页切换到后台,或从后台切换到前台时就会触发该消息,简单说相当于onhide

	watch: {
			'opening': {
				handler: function(newVal, oldVal) {
					if(newVal == false){
						document.addEventListener("visibilitychange",()=> {
						  if (this.timer) {
						    // this.opening = false
						    clearTimeout(this.timer)
						  }
						}, false);
					}
					
				},
				immediate:true
			}
		},

写好了之后记得需要打包自定义基座测试

也是第一次写,还是看到一个大佬的文章 写的

myplayUser(){
				//判断是否微信登陆 是的话直接跳到应用市场
				var u = navigator.userAgent;
				var isWeixin = u.toLowerCase().indexOf('micromessenger') !== -1; // 微信内
				if(isWeixin>-1){
					if (uni.getSystemInfoSync().platform == 'android') {
						 window.location = 'https://a.app.qq.com/o/simple.jsp?pkgname=xxxxxxxxxxx'
					}else if (uni.getSystemInfoSync().platform == 'ios'){
						 window.location = 'https://apps.apple.com/cn/app/idxxxxxxx'
					}
					return false
				}
				//若在其他浏览器 
				uni.showLoading({
					title:'跳转中...'
				})
				 let that = this
				  that.opening = true
				  if (uni.getSystemInfoSync().platform == 'android') { //  安卓处理
				    let ifr = document.createElement('iframe');
				    ifr.src = "nameAPP://";  //配置的app 链接"schemes" :  ["nameAPP"]
				    ifr.style.display = 'none';
				    document.body.appendChild(ifr); //如果可以已经安装就可以直接打开了
					
					that.timer = window.setTimeout(function () {  //  未安装的情况
				      that.opening = false
				      document.body.removeChild(ifr);
				      //  提示下载
					    uni.hideLoading()
				      let r = confirm("未安装APP? 是否下载")
				      if (r) {
				        window.location = 'https://a.app.qq.com/o/simple.jsp?pkgname=uni.zxxxxxxx'
				      }
				    }, 4000)
					
				  } else {  //  IOS处理
				    window.location = "nameAPP://"    //如果可以已经安装就可以直接打开了
				    that.timer = setTimeout(function () { //  未安装的情况。
				      that.opening = false
				      //  跳转app store
					    uni.hideLoading()
				      let r = confirm("未安装APP? 是否去App store查看")
				      if (r) {
				        window.location = 'https://apps.apple.com/cn/app/idxxxxxxx'
				      }
				    }, 4000);
				  }
			},
uniapp 中,不同场景下有不同唤起 app 进入前台的方法。 在安卓端,若要实现点击通知栏启动应用自身,代码一般写在 service 类中,通过如下代码实现: ```java NotificationChannel notificationChannel = null; notificationChannel = new NotificationChannel("important", "Important", NotificationManager.IMPORTANCE_DEFAULT); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); assert notificationManager != null; notificationManager.createNotificationChannel(notificationChannel); Intent intent=new Intent(this,io.dcloud.PandoraEntryActivity.class); PendingIntent pi=PendingIntent.getActivity(this,0,intent,0); Notification notification=new NotificationCompat.Builder(this,"important") .setContentTitle("测试") .setContentText("app正在运行") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.icon_basic) .setContentIntent(pi) .build(); startForeground(1,notification); ``` 如果是在 uniapp 开启的前台服务,点击通知栏想要启动 app,而 app 又没有 activity 的话,就要使用 `io.dcloud.PandoraEntryActivity` 作为要跳转的活动,关键代码为 `Intent intent=new Intent(this,io.dcloud.PandoraEntryActivity.class)` [^2]。 在 h5 唤起 app 方面,代码中的 `nameAPP://` 代表着 schemes/Universal Link 通用链接,也就是跳到自己 app 的快捷链接。若链接需要带参数,例如 `redxiang://params` 。在微信浏览器中,由于直接打开的 h5 链接不能直接唤起 app,需要在微信开放平台配置相关参数(若未配置可让其跳到应用市场)。目前可直接获取当前手机是 android 还是 ios [^3]。 对于 ios 下的配置,可参考如下内容: ```json "urltypes" : [ { "urlidentifier" : "cn.net.nftmall", "urlschemes" : [ "nftmall" ] } ], "urlschemewhitelist": [ "nftmall" ] ``` [^4]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值