本文原创发布在华为开发者社区,更多鸿蒙场景化示例请见华为开发者联盟官网“行业实践与常见问题”专题页。
介绍
本示例基于原生能力,实现对单个页面设置为隐私模式,使其可以禁止截屏,录屏及分享屏幕等行为。
效果预览
实现思路
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) })
需要设置防截屏页面:
@State windowClass: window.Window | null | undefined = AppStorage.get('windowClass') aboutToAppear(): void { let isPrivacyMode: boolean = true if (this.windowClass) { this.windowClass.setWindowPrivacyMode(isPrivacyMode); // 设置防截屏录屏 } }
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。