I'm using Cordova do build a hybrid app, and I'm trying to dynamically change the color of the status bar. The reason I'm doing it dynamically is so that different platforms can use slightly different colors to make it feel a bit more native.
I installed cordova-plugin-statusbar
in my project, and now I have the StatusBar
object in my app's JS code. However, when I try to use the StatusBar.backgroundColorByHexString
method, it doesn't work.
Here's the code that handles this:
function onDeviceReady() {
document.addEventListener( 'pause', onPause.bind( this ), false );
document.addEventListener( 'resume', onResume.bind( this ), false );
if (StatusBar) {
window.alert("The plugin loaded correctly.");
}
if (cordova.platformId == 'ios') {
StatusBar.overlaysWebView(false);
StatusBar.backgroundColorByHexString("#4CAF50");
window.alert("The plugin should have run.");
} else {
StatusBar.backgroundColorByHexString("#388E3C");
window.alert("The plugin should have run.");
}
};
The code runs without throwing any errors, yet it doesn't change the status bar at all (I'm testing on an Android device, and so I'm assuming the same behavior happens on other platforms). How can I fix this?
I finally figured it out, so if anyone else has this problem, here's the solution:
config.xml
file, and add a CSP policy tag such as <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
to every page in your app