How to block screenshot in flutter ios application?

92 views Asked by At

I am trying to block screenshot in my flutter ios app but it's not working. User is still able to take screenshot in his device. Could you please help me to implement blocking screenshot in my flutter app for ios? Thanks in advance.

Here you go with my AppDelegate.swift file code:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
     self.window.makeSecure()
     GeneratedPluginRegistrant.register(with: self)
     return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }


  // Screenshot Prevent Functions


  extension UIWindow {
  func makeSecure() {
      let field = UITextField()
      field.isSecureTextEntry = true
      self.addSubview(field)
      field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
      field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
      self.layer.superlayer?.addSublayer(field.layer)
      field.layer.sublayers?.first?.addSublayer(self.layer)
    }
  }
1

There are 1 answers

1
Farrukh Makhmudov On

Have u tried this one?

import Flutter
import UIKit

let CHANNEL = "disable_screenshots"

class AppDelegate: FlutterAppDelegate {
override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
    let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
    let channel = FlutterMethodChannel(name: CHANNEL, binaryMessenger: controller.binaryMessenger)
    
    channel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
        if (call.method == "disable") {
            UIScreen.main.isCapturedDidChange = { [weak self] in
                if UIScreen.main.isCaptured {
                    UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
                }
            }
            result(true)
        } else {
            result(FlutterMethodNotImplemented)
        }
    }
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}