咨询Flutter iOS应用防止截图的解决方案(已用flutter_windowmanager适配安卓)
Flutter iOS 防止截图解决方案
针对iOS平台,你可以通过以下几种方式实现防止截图/录屏的功能:
1. 核心实现:通过MethodChannel调用iOS原生API
iOS原生提供了UIView.isSecure属性,设置为true后,该视图将无法被截图或录屏。你可以通过Flutter的MethodChannel桥接原生代码实现:
Flutter 端代码
import 'package:flutter/services.dart'; class ScreenSecureManager { static const MethodChannel _channel = MethodChannel('your_app/screen_secure'); /// 开启防截图模式 static Future<void> enableSecureScreen() async { try { await _channel.invokeMethod('enableSecure'); } on PlatformException catch (e) { print('开启防截图失败: ${e.message}'); } } /// 关闭防截图模式 static Future<void> disableSecureScreen() async { try { await _channel.invokeMethod('disableSecure'); } on PlatformException catch (e) { print('关闭防截图失败: ${e.message}'); } } }
iOS 原生代码(AppDelegate.swift)
import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { let controller = window?.rootViewController as! FlutterViewController let channel = FlutterMethodChannel(name: "your_app/screen_secure", binaryMessenger: controller.binaryMessenger) channel.setMethodCallHandler { [weak self] call, result in guard let self = self else { return } switch call.method { case "enableSecure": self.updateSecureScreenStatus(enabled: true) result(nil) case "disableSecure": self.updateSecureScreenStatus(enabled: false) result(nil) default: result(FlutterMethodNotImplemented) } } GeneratedPluginRegistrant.register(with: self) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } /// 更新视图的安全状态 private func updateSecureScreenStatus(enabled: Bool) { guard let rootVC = window?.rootViewController else { return } // 给根视图控制器设置isSecure rootVC.view.isSecure = enabled // 如果是导航控制器,遍历所有子控制器设置 if let navVC = rootVC as? UINavigationController { navVC.viewControllers.forEach { $0.view.isSecure = enabled } } } }
2. 监听截图/录屏事件
如果需要在用户触发截图或录屏时做额外处理(比如弹窗提示),可以监听iOS的UIScreen.capturedDidChangeNotification通知:
iOS 原生端添加监听
在AppDelegate.swift中添加以下代码:
override func applicationDidBecomeActive(_ application: UIApplication) { super.applicationDidBecomeActive(application) NotificationCenter.default.addObserver( self, selector: #selector(onScreenCaptured), name: UIScreen.capturedDidChangeNotification, object: nil ) } override func applicationWillResignActive(_ application: UIApplication) { super.applicationWillResignActive(application) NotificationCenter.default.removeObserver( self, name: UIScreen.capturedDidChangeNotification, object: nil ) } @objc private func onScreenCaptured() { if UIScreen.main.isCaptured { // 通知Flutter端检测到截图/录屏 let channel = FlutterMethodChannel(name: "your_app/screen_secure", binaryMessenger: window!.rootViewController as! FlutterViewController) channel.invokeMethod("screenCaptured", arguments: nil) } }
Flutter 端监听事件
在需要处理的页面中添加监听:
@override void initState() { super.initState(); ScreenSecureManager._channel.setMethodCallHandler((call) async { if (call.method == 'screenCaptured') { // 弹出提示 showDialog( context: context, builder: (_) => AlertDialog( title: const Text('提示'), content: const Text('禁止截图,请遵守相关规定'), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('知道了'), ) ], ), ); } }); }
注意事项
UIView.isSecure会影响整个视图层级,如果你只需要对特定页面开启防截图,需要在页面进入/退出时分别调用开启/关闭方法。- 该属性无法完全阻止所有截图方式(比如第三方工具通过系统漏洞截图),但能覆盖系统自带的截图和录屏功能。
内容的提问来源于stack exchange,提问作者Vepa Sabyrow
相关产品推荐
相关产品推荐

