anti-screenshot (Android + iOS) 手机防截屏或者虚化,或是模糊处理页面

Android 提供了系统级 API 来禁止当前 Activity 截屏:只对当前 Activity 生效,切换页面需重新设置,无法阻止 root 设备或部分第三方工具的强制截屏。
// 在你的 WebView 所在的 Activity 中添加
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 禁止当前窗口截屏和录屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_webview);
}
IOS 没有直接的 "禁止截屏" API,监听截屏事件并做处理
// 在 AppDelegate 或 WebView 容器中监听
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDidTakeScreenshot:)
name:UIApplicationUserDidTakeScreenshotNotification
object:nil];
- (void)userDidTakeScreenshot:(NSNotification *)notification {
// 截屏后执行:
// 1. 提示用户禁止截屏
// 2. 模糊/隐藏敏感内容
// 3. 上报风控系统
}
使用 UITextField 遮挡敏感区域(更强限制),利用 iOS 系统对密码输入框的保护特性,将敏感内容包裹在 UITextField 中并设置 secureTextEntry = YES,系统会自动阻止对该区域的截屏和录屏。
H5 辅助防护手段,敏感内容模糊化 :当检测到页面失焦 / 进入后台时,用 filter: blur(8px) 模糊页面核心内容。
.sensitive-content {
transition: filter 0.3s;
}
.blur {
filter: blur(8px);
}
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
document.querySelector('.sensitive-content').classList.add('blur');
} else {
document.querySelector('.sensitive-content').classList.remove('blur');
}
});
在页面上叠加半透明用户水印,即使被截图也能溯源,与原生端通信,上报截屏事件到后台,做行为风控,任何防截屏方案都无法阻止物理拍照、录屏设备(如外接相机)。