5

Our app needs to prevent users to record our screen for some important reasons. How to prevent screen-record on iOS and android?

It seems that we can get a call-back if users are recording the screen after iOS 11. Is there any method to use that call-back to realize screen-record preventing?

Anthony
  • 71
  • 1
  • 3

3 Answers3

4

That is something which is not completely possible. There are 2 scenarios for Android OS

1 - When Android device is not rooted - You can use flag FLAG_SECURE which will prevent screenshot and video recording. You can read further here https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SECURE

2 - When Android device is rooted - You can have a check programatically to know if device is rooted or not. If it is rooted then you can stop your application from running further. Here is the link to check for root device - Determining if an Android device is rooted programmatically?

sanmeet
  • 200
  • 2
  • 13
  • 1
    There is no way to check if the device is rooted robustly. Any method in that post can be trivially broken. Not to mention dozens of other ways around it, including the unbreakable "point another camera at it and record on that one" method. Its a fools game to be trying this. – Gabe Sechan Aug 09 '19 at 15:16
  • Yes, it is not something which is fail safe. Above options are just good practices to follow if you are creating such application. – sanmeet Aug 09 '19 at 15:25
4

Put this code in your MainActivity.kt and You're done :) (The code isn't mine, I found it on Github)

    import io.flutter.embedding.android.FlutterActivity
    import android.os.Build
    import android.view.ViewTreeObserver
    import android.app.ActivityManager
    import android.content.Context
    import android.content.pm.ApplicationInfo
    import android.content.pm.PackageManager
    import android.os.Bundle
    import android.util.Log
    import android.view.SurfaceView
    import android.view.View
    import android.view.ViewGroup
    import android.view.WindowManager
    import android.view.WindowManager.LayoutParams
    import io.flutter.embedding.engine.FlutterEngine
    import io.flutter.plugin.common.MethodChannel
    
    class MainActivity: FlutterActivity() {
            override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
        val flutter_native_splash = true
        var originalStatusBarColor = 0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            originalStatusBarColor = window.statusBarColor
            window.statusBarColor = 0xffeaeaea.toInt()
        }
        val originalStatusBarColorFinal = originalStatusBarColor
    
    
            if (!setSecureSurfaceView()) {
                Log.e("MainActivity", "Could not secure the MainActivity!")
                // React as appropriate.
            }
            getWindow().addFlags(LayoutParams.FLAG_SECURE)
            getWindow().setFlags(LayoutParams.FLAG_SECURE,
                    LayoutParams.FLAG_SECURE)
            SurfaceView(applicationContext).setSecure(true)
        }
        private fun setSecureSurfaceView(): Boolean {
            val content = findViewById<ViewGroup>(android.R.id.content)
            if (!isNonEmptyContainer(content)) {
                return false
            }
            val splashView = content.getChildAt(0)
            if (!isNonEmptyContainer(splashView)) {
                return false
            }
            val flutterView = (splashView as ViewGroup).getChildAt(0)
            if (!isNonEmptyContainer(flutterView)) {
                return false
            }
            val surfaceView = (flutterView as ViewGroup).getChildAt(0)
            if (surfaceView !is SurfaceView) {
                return false
            }
            surfaceView.setSecure(true)
            this.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
            return true
        }
    
        private fun isNonEmptyContainer(view: View): Boolean {
            if (view !is ViewGroup) {
                return false
            }
            if (view.childCount < 1) {
                return false
            }
            return true
        }
    }
1

You can use the flutter_windowmanager plugin. Once you have added to your pubspec.yaml file then import it where you want to disable screenshot. Using

import 'package:flutter_windowmanager/flutter_windowmanager.dart';

Now add this line into your Stateful Widget.

Future<void> disableScreenShot() async {
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE); 
}
@override
void initState() {
  disableScreenShot();
 super.initState();
}

Now Normal Users can't take screenshots.

Shailendra Rajput
  • 2,131
  • 17
  • 26