InAppBrowser.open opens in browser instead of the WebView

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:

enter image description here

How the hell do I open the url in the Cordova webview itself?


I also tried with window.location="https://google.com", same behavior.

2 answers

  • answered 2017-11-12 19:45 bozdoz

    From the docs it looks like you need to set target to _blank to open in the InAppBrowser:

    cordova.InAppBrowser.open('https://google.com', '_blank');
    

    _self: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the InAppBrowser.

    _blank: Opens in the InAppBrowser.

    _system: Opens in the system's web browser.

  • answered 2017-11-12 22:40 Birowsky

    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: