The goal is to open a foreign url within the webview when the app starts.
I create fresh Cordova project:
cordova create test
cd test
cordova platform add ios
cordova plugin add cordova-plugin-inappbrowser
I inline this script in www/index.html
:
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
cordova.InAppBrowser.open('https://google.com', '_self');
}
</script>
I test the app with cordova run ios
, app starts, and I get this:
Refused to execute a script because its hash, its nonce, or 'unsafe-inline' appears in neither the script-src directive nor the default-src directive of the Content Security Policy.
So I add 'unsafe-inline'
to the Content Security Policy tag and it becomes this:
<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
media-src *;
img-src 'self' data: content:;">
I test the app with cordova run ios
, app starts, but it opens https://google.com
in Safari:
How the hell do I open the url in the Cordova webview itself?
I also tried with window.location="https://google.com"
, same behavior.
I was missing this in the config.xml
:
<allow-navigation href="*" />
I somehow missed it from the docs.
By default, navigations only to file:// URLs, are allowed. To allow others URLs, you must add tags to your config.xml: