Flutter : How to Disable Screenshot Capture for App (2023)

In app development, Sometimes due to security reasons, We need prevent to taking screenshots like in the banking app. So In this article, I  will share with you How to Disable Screenshot Capture for App.

How to disable screenshots for Android:

Let's see step by step to prevent screenshots for android. 

- The first step is to go to "MainActivity.kt" or "MainActivity.kt".You can find it from the below path. 

android\app\src\main\kotlin\com\example\auth_email\MainActivity.kt

 

- The second step is to put the below code in MainActivity.kt.

 window.addFlags(LayoutParams.FLAG_SECURE)

Finally, Your MainActivity.kt looks like the below.

import io.flutter.embedding.android.FlutterActivity
import android.view.WindowManager.LayoutParams
import io.flutter.embedding.engine.FlutterEngine

class MainActivity: FlutterActivity() {

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        window.addFlags(LayoutParams.FLAG_SECURE)
        super.configureFlutterEngine(flutterEngine)
    }

}

 

The last step is to restart your app now if you go to take a screenshot you can't take screenshots. Just follow the above steps so you can prevent screenshots for android.

 

Just follow the above steps so you can prevent screenshots for android.

 

How to disable screenshots for IOS:

- The first step is to go to "AppDelegate.swift".You can find it from the below path. 

ios\Runner\AppDelegate.swift

 

- The second step is to put the below code in AppDelegate.swift.

override func applicationWillResignActive(
      _ application: UIApplication
    ) {
      self.window.isHidden = true;
    }
    override func applicationDidBecomeActive(
      _ application: UIApplication
    ) {
      self.window.isHidden = false;
    }

 

The third step is we need to also add the below function in AppDelegate.swift. And Call this method into the didFinishLaunchingWithOptions function.

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

 

Finally, Your AppDelegate.swift looks like the below.

import UIKit
import Flutter
import FirebaseCore

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  private var textField = UITextField()
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    self.window.makeSecure()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  override func applicationWillResignActive(
    _ application: UIApplication
  ) {
    self.window.isHidden = true;
  }
  override func applicationDidBecomeActive(
    _ application: UIApplication
  ) {
    self.window.isHidden = false;
  }

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

 

The last step is to restart your app now if you go to take a screenshot you can't take screenshots. Just follow the above steps so you can prevent screenshots for IOS.

 

Conclusion:

In this article, we learned, How to Disable Screenshot Capture for App in flutter step by step for android and ios. I hope this article may help you.

 

Tags: