Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap Admob, external links... Can't get either one to work

I know that there are bunch of answers here concerning PhoneGap. I've gone through all of them, and not a single solution worked. Even though, by all accounts, any one of them should. Many of the answers are so old that I doubt they are even valid anymore.

My app works, compiles, a runs fine. Except that I cannot get ads working. I tried AdMob first. Ads never show. So I said, "fine... I'll make my own instead." Which would be great if clicking on the ad resulted in opening a new browser. I thought a simple window.open() command would work. But, no. That doesn't happen. I need to use a cordova plugin to make that work.

I am using PhoneGap-Build on the Adobe site.

In my config.xml, I have:

<gap:plugin name="cordova-plugin-inappbrowser" spec="~3.0.0" source="npm" />

When I update the source I can see that the plugin is showing:

(from the PhoneGap page) Plugin Source Version Installed Platforms cordova-plugin-inappbrowser npm ~3.0.0 3.0.0 android,ios,winphone

After 30 or so tries with different variations of this, here is the JS code that I last tried:

document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        window.open = cordova.InAppBrowser.open;
    }

    function navToMobile(where){
        var ref = cordova.InAppBrowser.open(where, '_system', 'location=yes');


        //These two lines are incorrect and causing errors.  The first doesn't do anything.  The second is only supposed to be called to show and existing window.  So the first line in this function is the only one that is needed.
        navigator.app.loadUrl(where, { openExternal:true });
        ref.show();

    }

The function call for "navToMobile(where)" sends in a URL for 'where'.

There are 2 different methods in that function that should work, according to previous answers. I have them both there hoping just one of them would work...

When this runs, nothing happens. I installed the app on an emulator so I could see the log cat. That tells me that "cordova is not available". This says to me that the library is not being compiled into the app.

From what I gather, if I were doing this without "build PhoneGap", I would have a cordova.js library as part of my project. But I thought that is what the reference in the config.xml is for...

Anyway... I suspect that the exact same thing happens with AdMob. I set everything up according to the instructions, I created a new banner ad with an ID in AdMob... And the ads never show.

If anyone has any ideas how to get either one of these methods working, that would be great.

EDIT: I am editing this answer to show what the final solution is.

As Dexter mentions in his answer, a reference must be made to the cordova.js file, without which, none of the plugin stuff will work. It would have been nice it they happened to mention this anywhere in their docs. They never once mention that the reference is necessary because THEY embed it at compile-time. You don't need to have that .js as part of your project.

With that working, I was getting errors. I knew this might be a problem because I had 3 different lines of code, and wasn't sure which would work.

I have edited the function in the code above to show the correct call.

like image 858
durbnpoisn Avatar asked Mar 10 '19 13:03

durbnpoisn


1 Answers

It sounds like you don't have <script src="cordova.js"></script> in your index.html, this file is added by Cordova during build time and is required for plugins to function.

like image 153
Dexter Avatar answered Sep 28 '22 19:09

Dexter