本文原创发布在华为开发者社区,更多鸿蒙场景化示例请见华为开发者联盟官网“行业实践与常见问题”专题页。

介绍

本示例基于原生能力,实现对单个页面设置为隐私模式,使其可以禁止截屏,录屏及分享屏幕等行为。

单页面防截屏源码链接

效果预览

请添加链接描述

实现思路

  1. onWindowStageCreate生命周期函数中获取windowClass对象并保存,在对应需要防截屏的页面获取对象并设置隐私模式:

     windowStage.getMainWindow((err: BusinessError, data) => {
      let errCode: number = err.code;
      if (errCode) {
      console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
      return;
      }
      const windowClass = data;
      console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
      AppStorage.setOrCreate("windowClass",windowClass)
    })
  2. 需要设置防截屏页面:

    @State windowClass: window.Window | null | undefined = AppStorage.get('windowClass')
    aboutToAppear(): void {
     let isPrivacyMode: boolean = true
     if (this.windowClass) {
       this.windowClass.setWindowPrivacyMode(isPrivacyMode); // 设置防截屏录屏
     }
      }

鸿蒙场景化代码
1 声望0 粉丝