微信小程序防录频截屏ios+android

ios截屏暂未解决

安卓录频截屏代码

可以写在onshow中

 // 安卓防止截屏录屏
    if (/android/i.test(wx.getSystemInfoSync().system)&&wx.setVisualEffectOnCapture) {
    
    
      wx.setVisualEffectOnCapture({
    
    
        visualEffect: 'hidden',
        complete: function (res) {
    
    
          // wx.showToast({
    
    
          //   title: '成功',
          // })
        }
      })
    }

但是要注意的是,一定不能只开启监听,也要卸载监听

  onHide() {
    
    
    //  页面隐藏和销毁时需要释放防截屏录屏设置
    // 安卓
    if (wx.setVisualEffectOnCapture) {
    
    
      wx.setVisualEffectOnCapture({
    
    
        visualEffect: 'none',
        complete: function (res) {
    
    }
      })
    }

  },
   onUnload() {
    
    
    //  页面隐藏和销毁时需要释放防截屏录屏设置
    if (wx.setVisualEffectOnCapture) {
    
    
      wx.setVisualEffectOnCapture({
    
    
        visualEffect: 'none',
        complete: function (res) {
    
    }
      })
    }

ios防录频

  onLoad(options) {
    
    
    //跨屏幕防止ios录频
    if (/ios/i.test(wx.getSystemInfoSync().system)&&wx.getScreenRecordingState) {
    
    
    // res.state的值有两种情况 on(正在录屏) | off(没有开始录屏)
      wx.getScreenRecordingState({
    
    
        success: res => {
    
    
          if (res.state != 'off') {
    
    
            that.setData({
    
    
              isShow: false//这里我对页面进行了遮挡处理
            })
          } else {
    
    
            that.setData({
    
    
              isShow: true
            })
          }
        }
      })
    }
    // 在当前页面点击录频
    if (/ios/i.test(wx.getSystemInfoSync().system)&&wx.onScreenRecordingStateChanged) {
    
    
    //	res.state的值有两种情况 start(开始录屏) | stop(结束录屏)
      wx.onScreenRecordingStateChanged(res => {
    
    
        if (res.state == 'start') {
    
    
          that.setData({
    
    
            isShow: false//这里我对页面进行了遮挡处理
          })
        
        } else {
    
    
          that.setData({
    
    
            isShow: true
          })
        }
      })
    }
  },

这里当然也要卸载

 onHide() {
    
    
    //  页面隐藏和销毁时需要释放防截屏录屏设置
    // ios
    if (wx.offScreenRecordingStateChanged) {
    
    
      // 取消录屏监听事件
      wx.offScreenRecordingStateChanged()
    }

  },
   onUnload() {
    
    
    //  页面隐藏和销毁时需要释放防截屏录屏设置
    // ios
    if (wx.offScreenRecordingStateChanged) {
    
    
      // 取消录屏监听事件
      wx.offScreenRecordingStateChanged()
    }

  },

猜你喜欢

转载自blog.csdn.net/Yannnnnm/article/details/130554859