1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

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

Discussão em 'Mobile' iniciado por Stack, Outubro 17, 2024 às 04:12.

  1. Stack

    Stack Membro Participativo

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

    }


    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!!

    Continue reading...

Compartilhe esta Página