Flutter如何在iOS设备上禁用截图或检测截图操作
Flutter iOS端截图检测/禁用可行解决方案
两种旧方案失效原因
- 旧的
screenshot_callback包久未更新,没有适配Android V2嵌入规范,所以会抛出废弃报错,已无法在新版Flutter项目中使用 - 你写的
applicationWillResignActive回调拦截逻辑是iOS7之前的旧实现,iOS7之后系统触发截图时不会调用该回调,因此完全没有效果
方案1:截图事件检测(仅捕获,无法拦截,可做后续处理)
iOS系统提供了专门的截图全局通知,监听该通知即可捕获用户的截图行为,再通过MethodChannel通知Flutter端执行自定义逻辑,比如弹出提示、清空敏感内容。
原生端实现代码
import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { private var methodChannel: FlutterMethodChannel? override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) // 初始化双向通信通道,通道名需和Flutter端保持一致 let rootVc = window?.rootViewController as! FlutterViewController methodChannel = FlutterMethodChannel(name: "com.your_package.screenshot_monitor", binaryMessenger: rootVc.binaryMessenger) // 注册系统截图通知监听 NotificationCenter.default.addObserver( self, selector: #selector(handleScreenshotEvent), name: UIApplication.userDidTakeScreenshotNotification, object: nil ) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } @objc private func handleScreenshotEvent() { // 通知Flutter端触发截图事件 methodChannel?.invokeMethod("screenshotCaptured", arguments: nil) } }
Flutter端监听代码
import 'package:flutter/services.dart'; class SensitivePage extends StatefulWidget { const SensitivePage({super.key}); @override State<SensitivePage> createState() => _SensitivePageState(); } class _SensitivePageState extends State<SensitivePage> { final MethodChannel _channel = const MethodChannel('com.your_package.screenshot_monitor'); @override void initState() { super.initState(); // 注册原生端回调监听 _channel.setMethodCallHandler((call) async { if (call.method == 'screenshotCaptured') { // 自定义截图后逻辑,比如弹出警告 if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('当前页面包含敏感内容,禁止截图')) ); } } }); } @override void dispose() { _channel.setMethodCallHandler(null); super.dispose(); } @override Widget build(BuildContext context) { // 页面内容 return const Scaffold(body: Center(child: Text('敏感页面'))); } }
方案2:完全禁用截图/录屏(iOS13+支持,截图/录屏结果为黑屏)
利用iOS系统UITextField的安全输入特性,开启isSecureTextEntry的视图在截图/录屏时会自动被系统屏蔽为黑屏,将该视图挂载到App根window下即可实现全App防截屏,也可通过Flutter端通信动态控制开关实现单页面防截屏。
原生端实现代码
import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { private let secureField = UITextField() override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) guard let window = self.window else { return super.application(application, didFinishLaunchingWithOptions: launchOptions) } // 开启安全输入模式 secureField.isSecureTextEntry = true // 禁止响应交互避免遮挡页面事件 secureField.isUserInteractionEnabled = false // 挂载到根window window.layer.addSublayer(secureField.layer) secureField.frame = window.bounds return super.application(application, didFinishLaunchingWithOptions: launchOptions) } }
注意事项
- 该方案仅支持iOS13及以上系统,更低版本无法生效
- 默认全局生效,如需单页面控制,可新增MethodChannel方法动态控制
secureField的显隐 - 对系统录屏、屏幕共享场景同样生效,所有内容都会被屏蔽为黑屏
开箱即用第三方包方案
如果不想手动编写原生桥接代码,可以使用当前仍在维护的screen_protector、screenshotx等pub包,均已适配最新Flutter版本和Android V2嵌入规范,不会出现废弃报错,集成后可快速实现跨平台的截屏拦截/检测功能。
内容的提问来源于stack exchange,提问作者Reda Kassad
相关产品推荐
相关产品推荐

