Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Cordova 2.7.0 JS seemingly no longer work on remote pages?

Background

I'm attempting to upgrade an iOS app built on Cordova 2.0 to version 2.7.

It's basically a welcome screen that points to a remote search engine (please withhold comments about app validity and likely approval, as we're past that), and we were using the ChildBrowser plugin to enable opening links in a sub browser so as not to trap the user in the Cordova webview.

Cordova 2.7 has a feature called InAppBrowser I am hoping to use instead of ChildBrowser. InAppBrowser does essentially the same thing, aside from missing a button to open in Safari.

Problem

The existing app's remote webpages include the Cordova JS (as well as that for the ChildBrowser plugin) and it works fine for opening links in the sub browser.

My test Cordova 2.7 app doesn't seem to load the Cordova JS correctly when it's being loaded from a remote web page.

I tried using this exact same HTML on the embedded start page and a remote start page:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://mydomain.com/mobile/cordova-2.7.0.js"></script>
  </head>
  <body>
    <script>
      document.addEventListener("deviceready", onDeviceReady, false);
      function onDeviceReady() {
        alert("Ready!!");
      }
    </script>
  </body>
</html>

To test this as the embedded start page, I put this line in config.xml:

<content src="index.html" />

When I run the app, I promptly get the "Ready!" alert.

To test this as the remote start page (I'm aiming to link to the remote page in the final app, I am just using it as the start page for testing. The result is the same if I link from the embedded page.), I put this line in config.xml:

<content src="http://mydomain.com/mobile/index.php" />

When I run the app, I just get the blank screen and no alert.

Further, in cordova-2.7.0.js L. 6255, I changed

console.log('deviceready has not fired after 5 seconds.');

to

alert('deviceready has not fired after 5 seconds.');

With that change, running the app using the remote start page causes the blank page, and then after five seconds, I get the alert "deviceready has not fired after 5 seconds.". So this tells me Cordova JS is not starting correctly. Needless to say, I can't get InAppBrowser to launch links in the sub browser on the remote site, but I can get it working just fine on the embedded start page.

Anyone have any ideas of where to go from here? This is a pretty simplistic example, so I'm assuming this is a Cordova settings problem or a change in the functionality. I appreciate any thoughts, thanks!

like image 737
Charlie Gorichanaz Avatar asked May 01 '13 03:05

Charlie Gorichanaz


2 Answers

Yes, something broke in 2.7 - related to our cordova-cli work. See: https://issues.apache.org/jira/browse/CB-3029

The fix is to add an empty file called "cordova_plugins.json" in your root folder.

like image 117
Shazron Avatar answered Nov 04 '22 22:11

Shazron


I had a similar problem relating to upgrading to Cordova 2.7. However my problem was all my console.logs stopped firing when running the app. I couldn't figure out why for the life of me this was happening. I thought it was because I upgraded jquery.mobile. That wasn't it. I then thought it was an .htaccess issue, that wasn't it either. It turns out, it was Cordova 2.7 that was causing this problem.

I did try adding the .json file on my server, that did not fix the issue.

The fix was going into the 2.7 source and commenting out the following code:

/*comment out this as it is breaking console.logs
    var xhr = new context.XMLHttpRequest();
    xhr.onload = function() {
        // If the response is a JSON string which composes an array, call handlePluginsObject.
        // If the request fails, or the response is not a JSON array, just call finishPluginLoading.
        var obj = this.responseText && JSON.parse(this.responseText);
        if (obj && obj instanceof Array && obj.length > 0) {
            handlePluginsObject(obj);
        } else {
            finishPluginLoading();
        }
    };
    xhr.onerror = function() {
        finishPluginLoading();
    };
    xhr.open('GET', 'cordova_plugins.json', true); // Async
    xhr.send();
    */

Replace entire block with a call to the following function:

finishPluginLoading();

My logs are now working again. Only took me 3 days scratching my head.

Hope this helps someone with a similar problem.

like image 41
jjsquared Avatar answered Nov 04 '22 20:11

jjsquared