Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 607
Daniel Birowsky Popeski Avatar asked Jan 03 '23 05:01

Daniel Birowsky Popeski


1 Answers

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:

like image 120
Daniel Birowsky Popeski Avatar answered Jan 17 '23 07:01

Daniel Birowsky Popeski