How to block Screenshots in your flutter app

when building highly secured apps, it can be essential to stop the user from taking screenshots, while this is quite straightforward on Android, it gets a little bit tricky on the iOS part. I remember facing this challenge some months ago and I scanned…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Odinachi David

when building highly secured apps, it can be essential to stop the user from taking screenshots, while this is quite straightforward on Android, it gets a little bit tricky on the iOS part. I remember facing this challenge some months ago and I scanned through a couple of StackOverflow answers for hours, I was at the point of getting frustrated when I stumbled on one that was the solution for the iOS part:

For Android:

  1. inside your mainActivity.(java/tk), import the following:
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import android.view.WindowManager.LayoutParams
  1. and you replace the content with:
class MainActivity: FlutterFragmentActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        window.addFlags(LayoutParams.FLAG_SECURE)
        GeneratedPluginRegistrant.registerWith(flutterEngine)
    }
}

this does it for Android.

For iOS:

  1. in your AppDelegate.swift you should create a window extension just like below:
  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)
    }
  }

then call the new window extension in your application function:

self.window.makeSecure()

your AppDelegate.swift should look like this:

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

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

Source: stackoverflow answer

thank you for reading, I hope this was helpful.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Odinachi David


Print Share Comment Cite Upload Translate Updates
APA

Odinachi David | Sciencx (2022-09-20T14:12:27+00:00) How to block Screenshots in your flutter app. Retrieved from https://www.scien.cx/2022/09/20/how-to-block-screenshots-in-your-flutter-app/

MLA
" » How to block Screenshots in your flutter app." Odinachi David | Sciencx - Tuesday September 20, 2022, https://www.scien.cx/2022/09/20/how-to-block-screenshots-in-your-flutter-app/
HARVARD
Odinachi David | Sciencx Tuesday September 20, 2022 » How to block Screenshots in your flutter app., viewed ,<https://www.scien.cx/2022/09/20/how-to-block-screenshots-in-your-flutter-app/>
VANCOUVER
Odinachi David | Sciencx - » How to block Screenshots in your flutter app. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/20/how-to-block-screenshots-in-your-flutter-app/
CHICAGO
" » How to block Screenshots in your flutter app." Odinachi David | Sciencx - Accessed . https://www.scien.cx/2022/09/20/how-to-block-screenshots-in-your-flutter-app/
IEEE
" » How to block Screenshots in your flutter app." Odinachi David | Sciencx [Online]. Available: https://www.scien.cx/2022/09/20/how-to-block-screenshots-in-your-flutter-app/. [Accessed: ]
rf:citation
» How to block Screenshots in your flutter app | Odinachi David | Sciencx | https://www.scien.cx/2022/09/20/how-to-block-screenshots-in-your-flutter-app/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.