I am making a Flutter app and I need to make sure the user is not able to capture screenshot of the app (any screen). Is there any way to achieve this in Flutter or do I need to write native code for both Android and IOS?
-
Is that even possible at all? – Rémi Rousselet Sep 13 '18 at 15:47
-
1I don't know, thats why I am asking here. – codeinprogress Sep 13 '18 at 15:52
-
In flutter it's currently unavailable, but you can write native code for Android & iOS as you said and maybe create a request on the GitHub of flutter ;) – huextrat Sep 13 '18 at 17:25
-
1If it is possible natively in Android and iOS, it should be possible in Flutter by writing a plugin or using channels. But first you must be sure it can be done natively. – chemamolins Sep 13 '18 at 21:19
-
3Relevant URL: [FLAG_SECURE](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SECURE) – Zroq Sep 14 '18 at 14:03
-
@chemamolins It is possible in Native Android and iOS – KZoNE Sep 26 '19 at 05:53
-
@codeinprogress It's works for me. You can try my answer. https://stackoverflow.com/a/72283925/9331686 – Trupesh Vaghasiya May 23 '22 at 05:07
14 Answers
- Locate your MainActivity class inside the embedded android project dir in your Flutter Project
- Add the following import to your main activity class:
import android.view.WindowManager.LayoutParams; - Add the following line to your MainActivity's onCreate method:
getWindow().addFlags(LayoutParams.FLAG_SECURE);
- 3,963
- 3
- 21
- 42
- 403
- 4
- 8
-
14
-
-
1
-
-
@Rebar for iOS maybe https://mrgulshanyadav.medium.com/prevent-screenshot-and-video-recording-in-flutter-93839325d66c – Marcosicp May 07 '21 at 06:52
-
-
For Flutter2 project
Method 1 : using package flutter_windowmanager
Method 2 :
in Android with kotlin
Step 1 Open the file "mainActivity.kt" using the path
android\app\src\main\kotlin\com\example\auth_email\MainActivity.kt
Step 2 Import Library
import android.view.WindowManager.LayoutParams
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
Step 3 In main activity class
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
window.addFlags(LayoutParams.FLAG_SECURE)
super.configureFlutterEngine(flutterEngine)
}
}
In iOS Swift : AppDelegate.swift file
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// <Add>
override func applicationWillResignActive(
_ application: UIApplication
) {
self.window.isHidden = true;
}
override func applicationDidBecomeActive(
_ application: UIApplication
) {
self.window.isHidden = false;
}
}
- 6,843
- 5
- 26
- 54
- 437
- 5
- 9
-
-
-
what if I want to allow screenshots but want to hide screen on recent tab window ??? for IOS and Android – Arslan Kaleem Dec 07 '21 at 18:18
-
-
The simplest way to do this is to use a flutter package called flutter_windowmanager
Works only for Android, not for IOS!
First import its latest version in pubspec.yaml file of your Flutter project and run pub get. Then add the below code inside the widget's initState() method for which you want to disable screenshot and screen recording.
Future<void> secureScreen() async {
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
}
@override
void initState() {
secureScreen();
super.initState();
}
@override
void dispose(){
super.dispose();
await FlutterWindowManager.clearFlags(FlutterWindowManager.FLAG_SECURE);
}
If you want to make your whole app screenshot disable just call securescreen() method (defined above) inside your main() function in main.dart file.
- 700
- 5
- 13
-
1I don't acutally know whether it work for IOS but yes it works very well for android. – Priyansh jain Feb 25 '21 at 06:26
-
1@Priyanshjain dear, you forgot to call `clearFlags` in `dispose()` method - `await FlutterWindowManager.clearFlags( FlutterWindowManager.FLAG_SECURE);` Otherwise it will restrict to take screenshot to all further screens/pages. Please update your answer. Thanks alot. – Kamlesh Aug 12 '21 at 07:23
-
-
-
What worked for me was writing the below code in MainActivity.java file.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}
and importing these packages!
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.os.Bundle; // required for onCreate parameter
- 451
- 1
- 7
- 19
It's only in iOS,just modify in AppDelegate. And no more plugins
import UIKit
import Flutter
import Firebase
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
self.window.makeSecure() //Add this line
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
//And this extension
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)
}
}
- 89
- 1
- 4
-
2
-
-
NOTE: you should change value of `isSecureTextEntry` in app lifecycle methods as I did in my answer. Otherwise you will get side effect: "red screen". Actions: go to background, switch to another app and back to your app (preview of your app will be red). – Volodymyr Mar 07 '22 at 16:06
-
Yes sir, it just not work in my case, I figure out in my way as ur code really help me a lot, also I don't know should changed lifecycle methods, thanks for tips sir. – ClarkCluster Mar 08 '22 at 01:16
-
1
Screenshots can be prevented very easily by following below two steps.
I am using VS code.
Step 1 Open the file "mainActivity.kt" using the path android\app\src\main\kotlin\com\example\auth_email\MainActivity.kt
Step 2 Add the two lines
(a) import android.view.WindowManager.LayoutParams;
(b) getWindow().addFlags(LayoutParams.FLAG_SECURE); in MainActivity: FlutterActivity() section
Restart the app
- 8,317
- 4
- 15
- 36
- 111
- 1
- 5
On iOS I have disabled taking of screenshots with the help of extension https://stackoverflow.com/a/67054892/4899849. Follow next steps:
Add property in
AppDelegate:var field = UITextField()in
didFinishLaunchingWithOptionscall next method:addSecuredView()private func addSecuredView() { if (!window.subviews.contains(field)) { window.addSubview(field) field.centerYAnchor.constraint(equalTo: window.centerYAnchor).isActive = true field.centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true window.layer.superlayer?.addSublayer(field.layer) field.layer.sublayers?.first?.addSublayer(window.layer) }}
override delegate methods:
override func applicationWillResignActive(_ application: UIApplication) { field.isSecureTextEntry = false } override func applicationDidBecomeActive(_ application: UIApplication) { field.isSecureTextEntry = true }
Now, when you make a screenshot in the app or record a screen video you will see a black image or video. Hope, it will help cuz I spent 2 days trying to make it work)
- 1,017
- 20
- 39
This works for iOS. In your Runner > AppDelegate.m:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
// Override point for customization after application launch.
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)applicationWillResignActive:(UIApplication *)application{
self.window.hidden = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
self.window.hidden = NO;
}
@end
- 1,603
- 19
- 23
-
-
1`AppDelegate.m` in case your iOS project is in ObjectiveC. There must be an equivalent for `AppDelegate.swift` in case your iOS project is in Swift :) – Rafa0809 Apr 03 '20 at 18:23
-
what is the [GeneratedPluginRegistrant registerWithRegistry:self]; i'm using native ios code – Amr Angry Apr 22 '20 at 11:35
-
2
-
If you are using kotlin
open MainActivity.kt
Add below code at end of imports
import android.view.WindowManager.LayoutParams
Add below code at end of super.onCreate(savedInstanceState)
window.addFlags(LayoutParams.FLAG_SECURE)
Its done.
- 276
- 1
- 3
- 9
Flutter
In Whole your Application
Open AppDelegate file and add UITextField variable.
private var textField = UITextField()
Create function in AppDelegate file.
// Screenshot Prevent Functions
private func makeSecureYourScreen() {
if (!self.window.subviews.contains(textField)) {
self.window.addSubview(textField)
textField.centerYAnchor.constraint(equalTo: self.window.centerYAnchor).isActive = true
textField.centerXAnchor.constraint(equalTo: self.window.centerXAnchor).isActive = true
self.window.layer.superlayer?.addSublayer(textField.layer)
textField.layer.sublayers?.first?.addSublayer(self.window.layer)
}
}
Call this method into the didFinishLaunchingWithOptions function.
makeSecureYourScreen()
In Specific Screen - Using Method Channel Open
AppDelegatefile and addUITextFieldvariable.
private var textField = UITextField()
Create function in AppDelegate file.
// Screenshot Prevent Functions
private func makeSecureYourScreen() {
if (!self.window.subviews.contains(textField)) {
self.window.addSubview(textField)
textField.centerYAnchor.constraint(equalTo: self.window.centerYAnchor).isActive = true
textField.centerXAnchor.constraint(equalTo: self.window.centerXAnchor).isActive = true
self.window.layer.superlayer?.addSublayer(textField.layer)
textField.layer.sublayers?.first?.addSublayer(self.window.layer)
}
}
Call this method into the didFinishLaunchingWithOptions function.
makeSecureYourScreen()
Also add your method channel code in didFinishLaunchingWithOptions function.
let controller : FlutterViewController = self.window?.rootViewController as! FlutterViewController
let securityChannel = FlutterMethodChannel(name: "secureScreenshotChannel", binaryMessenger: controller.binaryMessenger)
securityChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
if call.method == "secureiOS" {
self.textField.isSecureTextEntry = true
} else if call.method == "unSecureiOS" {
self.textField.isSecureTextEntry = false
}
})
Add your code below code to your flutter files to disable the screenshot on a specific screen.
// Declare your method channel varibale here
var iosSecureScreenShotChannel = const MethodChannel('secureScreenshotChannel');
Now add code to initState for prevent screenshot
@override
void initState() {
// this method to user can't take screenshot your application
iosSecureScreenShotChannel.invokeMethod("secureiOS");
// TODO: implement initState
super.initState();
}
For add code to dispose for allow screenshot in another screen.
@override
void dispose() {
// this method to user can take screenshot your application
iosSecureScreenShotChannel.invokeMethod("unSecureiOS");
// TODO: implement dispose
super.dispose();
}
You can disable screenshots and video captures like the Netflix app and Disney Hotstar app.
I have tried in my application and it's works fine.
- 261
- 2
- 9
try to use
for android edit MainActivity.kt
package com.package.app_name
import android.view.WindowManager.LayoutParams
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
window.addFlags(LayoutParams.FLAG_SECURE)
super.configureFlutterEngine(flutterEngine)
}
}
- 4,036
- 3
- 11
- 19
- define this package inside
pubspec.yamlfile
flutter_windowmanager: ^0.0.1+1
- get dependencies
flutter pub get
- You need to call a method of
FlutterWindowManagerusingawaitandasync. - You have to add a few lines of code in your
StatefulWidget.
Future<void> secureScreen() async {
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
}
@override
void initState() {
secureScreen();
super.initState();
}
- 1,130
- 10
- 14
- 21
-
-
-
@b.john after spending some time I realised that Apple doesn't provide any official API to prevent screenshot capturing or recording. So I have left it out and only implemented it for android. – sagar suri Jun 22 '21 at 06:09
https://pub.dev/packages/screen_protector
use this one. works for Android, iOS both. In iOS, screenshot will be captured but output will be black screen.
- 84
- 2
- 9
You could maybe listen for the screenshot keys on iOS and when the combination is being pressed blacken the screen.
- 114
- 4