Is it possible to call / view cordova plugin from the android application of an external website that is opened within the app?

1

I have a simple app using the cordova, inside, I call an external web application developed in AngularJS. I want to start monetization in the app, and for that I want to make sales through the Google Play Store, inside of it. To do this, I need to within that external web application, call the javascript / plugin inAppPurchase that has already been added to the project.

It happens, of course, that is not directly possible. Currently, my call to the external application looks like this:

<script>
  document.addEventListener("deviceready", onDeviceReady, false);
  function onDeviceReady() {
    if (navigator.network.connection.type == Connection.NONE) {
      networkError()
    } else {
      loadApp()
    }
  }

  function loadApp() {
    navigator.app.loadUrl("http://192.168.0.102:8000/")
  }
</script>

The plugin I want to call: InAppPurchase - link

Calling the index.html of the cordova project it works fine, now from the external application, it presents the following error:

  

Uncaught ReferenceError: inAppPurchase is not defined

The call in the external project index that is called within the app looks like this:

<script>
  inAppPurchase.getProducts(['vaga_de_veiculo'])
    .then(function (products) {
      console.log(products);
    })
  .catch(function (err) {
    console.log(err);
  });
</script>

I'm trying now with inAppBrowser, but I was not successful either.

Thanks for any help!

    
asked by anonymous 17.05.2016 / 23:30

1 answer

0

I solved the problem, I hope it helps others. SOLUTION WORKS ONLY FOR ANDROID.

The call in index.html in the www folder of the cordova project was as follows:

<script>
  document.addEventListener("deviceready", onDeviceReady, false);
  function onDeviceReady() {
    if (navigator.network.connection.type == Connection.NONE) {
      networkError()
    } else {
      loadApp()
    }
  }

  function loadApp() {
    window.open("http://192.168.0.102:8000/", "_system");
  }
</script>

Config.xml needed to have the following lines entered:

<access origin="*" subdomains="true"/>
<allow-navigation href="http://192.168.0.102:8000/*" />

I needed to change the SystemWebViewClient.java class of the cordova. It is located in \ platforms \ android \ CordovaLib \ src \ org \ apache \ cordova \ engine \ SystemWebViewClient.java by adding the following statements:

Declare variable that will be injected into the javascript call in your external application:

private static final String INJECTION_TOKEN = "**injection**";

Locate the method: shouldInterceptRequest and then try {) add the following code:

if(url != null && url.contains(INJECTION_TOKEN)) {
String assetPath = url.substring(url.indexOf(INJECTION_TOKEN) + INJECTION_TOKEN.length(), url.length());
try {
    String mimeType = "text/plain";

    String ext = assetPath.substring(assetPath.lastIndexOf(".") + 1, assetPath.length());
    WebExtension extension = WebExtension.valueOf(ext.toUpperCase());

    switch(extension) {
        case PNG:
            mimeType = "image/png";
            break;
        case MP3:
            mimeType = "audio/mpeg";
            break;
        case MP4:
            mimeType = "video/mp4";
            break;
        case TTF:
            mimeType = "application/x-font-ttf";
            break;
        case SVG:
            mimeType = "image/svg+xml";
            break;
        case JS:
            mimeType = "application/javascript";
            break;
        case ICO:
            mimeType = "image/x-icon";
            break;
        case HTML:
            mimeType = "text/html";
            break;
        case CSS:
            mimeType = "text/css";
            break;
        case EOT:
            mimeType = "application/vnd.ms-fontobject";
            break;
        case WOFF:
            mimeType = "application/x-font-woff";
            break;
        case JSON:
            mimeType = "application/json";
            break;
    }

    WebResourceResponse response = new WebResourceResponse(
        mimeType,
        "UTF-8",
        parentEngine.webView.getContext().getAssets().open(assetPath)
    );
    return response;
} catch (IOException e) {
    e.printStackTrace(); // Failed to load asset file
}}

Configuration finished in now in your external web project , you add the following call, in index.html (if applicable):

<script type="text/javascript" src="**injection**www/cordova.js"></script>

In this way, your web project can access the Cordova APIs. I tested the inAppPurchase plugin and it worked fine.

    
19.05.2016 / 16:38