Learn how to disable the screenshots in a cordova application easily.

You may want to disable the screenshots to prevent the copy of your image content or videos etc. This task is easy to achieve in Java, however as a cordova developer you may want to use a plugin instead.

Note : If you need support for Android and iOS, go to the iOS support plugin title at the end of the article instead.

Requirements

We'll need the cordova-ourcodeworld-preventscreenshots plugin to disable dinamically the permission of screenshots.

To install it, execute the following command in the command prompt of node.js :

cordova plugin add https://github.com/sdkcarlos/cordova-ourcodeworld-preventscreenshots.git

Then in javascript you'll able to use the plugin with the global variable OurCodeWorldpreventscreenshots. This plugin sets the FLAG_SECURE flag in Android (which also prevents manual screenshots from being taken). Read more about the plugin here.

Disable screenshots

To disable the screenshots in your app, use the disable method provided by the plugin. Note that the script needs to be executed during of after the deviceready event of cordova.

document.addEventListener("deviceready", function(){
    var successCallback = function(){
        console.log("The screenshots are not allowed now.");
    };

    var errorCallback = function(err){
        console.error("An error ocurred : " + err);
    };

    OurCodeWorldpreventscreenshots.disable(successCallback,errorCallback);
}, false);

If the user try to take a snapshot, a message will appear that warns that the action is now allowed due to the secure policy as netflix or Google Movies app does. 

Prevent screenshots android cordova

The app will take a gray background if is seen from the recently used apps.

 Cordova screenshots recently used apps

Enable screenshots again

The plugin allow you to dinamically change the permission of the screenshots on your app. To enable the screenshots again use the enable method.

document.addEventListener("deviceready", function(){
    var successCallback = function(){
        console.log("The screenshots are allowed now again.");
    };

    var errorCallback = function(err){
        console.error("An error ocurred : " + err);
    };

    OurCodeWorldpreventscreenshots.enable(successCallback,errorCallback);
}, false);

Compatibility with iOS

Pitifully, ourcodeworld-cordova-preventscreenshots plugin doesn't support iOS but there's another plugin that supports both Android and iOS, however you cannot change dinamically this feature. Read the documentation of the plugin here.

Install the following plugin to provide support for both iOS and Android :

cordova plugin add cordova-plugin-privacyscreen 
# or 
phonegap local plugin add cordova-plugin-privacyscreen

Note that you don't need to do anything but install the plugin as it will be automatically configured on the installation. The privacy will be enabled forever in your app.

Have fun


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors