your_flutter_project/macos/Runner/AppDelegate.swift
`import Cocoa
import FlutterMacOS
@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
override func applicationDidFinishLaunching(_ aNotification: Notification) {
let controller: FlutterViewController = mainFlutterWindow?.contentViewController as! FlutterViewController
let screenshotBlockerChannel = FlutterMethodChannel(name: "screenshot_blocker", binaryMessenger: controller.engine.binaryMessenger)
screenshotBlockerChannel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
if call.method == "disableScreenshots" {
self.disableScreenshots(result: result)
} else if call.method == "enableScreenshots" {
self.enableScreenshots(result: result)
} else {
result(FlutterMethodNotImplemented)
}
}
super.applicationDidFinishLaunching(aNotification)
}
// Disable Screenshots Logic
private func disableScreenshots(result: FlutterResult) {
if let window = mainFlutterWindow {
// Setting the security level for the window
window.sharingType = .none // Disable window sharing (prevents some forms of screen capture)
result(true)
} else {
result(FlutterError(code: "UNAVAILABLE", message: "Window not available", details: nil))
}
}
// Enable Screenshots Logic (restore default behavior)
private func enableScreenshots(result: FlutterResult) {
if let window = mainFlutterWindow {
// Restore window sharing behavior
window.sharingType = .readWrite
result(true)
} else {
result(FlutterError(code: "UNAVAILABLE", message: "Window not available", details: nil))
}
}
}
`
import 'package:flutter/services.dart';
class ScreenshotBlocker {
static const MethodChannel _channel = MethodChannel('screenshot_blocker');
// Method to disable screenshots
static Future<void> disableScreenshots() async {
try {
await _channel.invokeMethod('disableScreenshots');
} on PlatformException catch (e) {
print("Failed to disable screenshots: '${e.message}'.");
}
}
// Method to enable screenshots (undo prevention)
static Future<void> enableScreenshots() async {
try {
await _channel.invokeMethod('enableScreenshots');
} on PlatformException catch (e) {
print("Failed to enable screenshots: '${e.message}'.");
}
}
}
@override
void initState() {
super.initState();
// To disable screenshots
ScreenshotBlocker.disableScreenshots();
}
我应该在哪里同时阻止截图和屏幕录制?目前,屏幕截图防护工作正常,但我还需要限制 macOS 和 Windows 桌面应用程序上的屏幕录制。提供的代码适用于 macOS。如果您也有适用于 Windows 的解决方案,请分享适用于这两个平台的解决方案。
提前致谢!
更新 AppDelegate.swift:
private func restrictScreenRecording() {
guard let window = mainFlutterWindow else { return }
window.sharingType = .none
}
override func applicationDidFinishLaunching(_ aNotification: Notification) {
let controller: FlutterViewController = mainFlutterWindow?.contentViewController as! FlutterViewController
let screenshotBlockerChannel = FlutterMethodChannel(name: "screenshot_blocker", binaryMessenger: controller.engine.binaryMessenger)
screenshotBlockerChannel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
if call.method == "disableScreenshots" {
self.disableScreenshots(result: result)
self.restrictScreenRecording()
} else if call.method == "enableScreenshots" {
self.enableScreenshots(result: result)
} else {
result(FlutterMethodNotImplemented)
}
}
super.applicationDidFinishLaunching(aNotification)
}
首先适用于Windows 在 Windows 项目中创建一个新的 C++ 文件 (ScreenBlocker.cpp):
#include <Windows.h>
#include <flutter/flutter_view_controller.h>
void DisableScreenRecording() {
SetProcessDPIAware();
}
然后修改现有的 Flutter Windows 集成:
在您的 Runner.cpp 中,将这些函数集成到您现有的方法通道设置中:
void MethodChannelHandler(const std::string& method) {
if (method == "disableScreenshots") {
DisableScreenRecording();
}
}