GNANA PRAKASH
GNANA PRAKASH

Reputation: 1

Prevent screen shot and screen record in MacOs and Windows - Flutter Desktop App

Where should I prevent both screenshots and screen recording? Currently, screenshot prevention is working fine, but I also need to restrict screen recording on macOS and Windows desktop apps.

The code provided is for macOS. If you have any solution for Windows as well, please share it for both platforms.

Thanks in Advance!!

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();
   
  }

Upvotes: -1

Views: 153

Answers (1)

Shoua Ul Qammar
Shoua Ul Qammar

Reputation: 233

Update the AppDelegate.swift:

private func restrictScreenRecording() {
    guard let window = mainFlutterWindow else { return }
    window.level = NSWindow.Level(CGShieldingWindowLevel())
    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)
}

For Windows Firstly Create a new C++ file (ScreenBlocker.cpp) in your Windows project:

#include <Windows.h>
#include <flutter/flutter_view_controller.h>

void DisableScreenRecording() {
    
    SetProcessDPIAware();
}

Then Modify your existing Flutter Windows integration:

In your Runner.cpp, integrate these functions into your existing method channel setup:

void MethodChannelHandler(const std::string& method) {
    if (method == "disableScreenshots") {
        DisableScreenRecording();
    }
}

Upvotes: 1

Related Questions