Securing app from screen recording in Flutter.
There is no doubt that everyone who develops video app want to protect the content from being recorded. So with packages there are solutions for this. But there are separate solutions for each platforms(android/iOS).
Android:
- Add flutter_windowmanager plugin into your dependencies.
- import
'package:flutter_windowmanager/flutter_windowmanager.dart';
- Add this code to your in main method:
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
or you can use it in initstate like below:
secureAndroid()async{
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
}
@override
initState(){
secureAndroid();
super.initState();
}
iOS:
- Add flutter_prevent_screen_capture to your dependencies.
- import
'package:flutter_prevent_screen_capture/flutter_prevent_screen_capture.dart';
- Declare variables above build function:
class _MyHomePageState extends State<MyHomePage> {
///Define a streamSubscription in order to receive changes
late StreamSubscription<bool> _screenRecordsSubscription;
///Get the instance of plugin for multiple use.
FlutterPreventScreenCapture preventScreenCapture =
FlutterPreventScreenCapture();
///is Recording is set to false initially.
bool isRecording = false;
4. Initialize listener function in initstate:
@override
void initState() {
///Though listening to the screen record, it is recommended to check the screen record status on the first launch.
checkScreenRecord();
///Initialize screenRecordSubscription to regularly listen to the changes
_screenRecordsSubscription =
preventScreenCapture.screenRecordsIOS.listen(updateRecordStatus);
super.initState();
}
updateRecordStatus(bool record) {
isRecording = record;
setState(() {});
}
Future<void> checkScreenRecord() async {
final recordStatus = await preventScreenCapture.checkScreenRecord();
debugPrint('Is screen being recorded: $recordStatus');
isRecording = recordStatus;
setState(() {});
}
5. It is recommended to check the screen record once initially because if the device screen record has begun before opening the app, the listener function may not trigger. So we are using the checkScreenRecord method to check screen record once initially.
Conclusion
If the post has been useful for you, you can follow me in order to see the next posts Immediately.