7

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.

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30
  • Can't say anything about AdMob, but I haven't had any issues with the InAppbrowser in combination with PhoneGap Build. Could you perhaps share your `index.html`? Are you including `cordova.js` in there? Are other Cordova plugins working? – Dexter Mar 18 '19 at 09:30
  • I've integrated AdMob a couple of months ago using this plugin https://github.com/ratson/cordova-plugin-admob-free , it works fine ( banner, interstitial and video ), the most difficult part is to configure the application in the AdMob website. Did you already try the admob-free plugin? – Sergio Rinaudo Mar 19 '19 at 15:11
  • @Dexter - I do not have cordova.js. Since I am using the online Adobe Build, it shows the cordova plugin there. Same thing with the admob plugin. You make a reference to it in the Config.xml and it shows up in the control panel. What I don't know is if that means it's being compiled into the app. Evidently not. – durbnpoisn Mar 19 '19 at 19:46
  • I think that the whole problem here is really going to boil down to no one being interested in using the online Adobe Build any more. Considering how long it took for me to get any responses to this question, that seems to be the case. For me, it was an experiment. And I got it working and released on Google Play - just with no ads. If I can work this out, I'll updated it. If not... Free app for everyone! – durbnpoisn Mar 19 '19 at 19:48
  • So do you have `` in your `index.html`? If not, please add it, plugins won't work without it. – Dexter Mar 20 '19 at 08:39
  • @Dexter - but I do not have the .js file. Are you saying that I need to include that line so that it imports it properly from elsewhere? – durbnpoisn Mar 20 '19 at 15:36
  • Yes, exactly. The file is added by Cordova, just add the line and try it out. – Dexter Mar 20 '19 at 19:13
  • Well, what do you know... That seems to have worked. The app crashes when you click the external link, so I have to figure that out. But it launches the link. So that's a step in the right direction. Add your comment there as an answer so I can award your bounty. – durbnpoisn Mar 21 '19 at 01:06

1 Answers1

4

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.

Dexter
  • 2,462
  • 4
  • 24
  • 28
  • I cannot award the bounty for 24 hours. But it's coming. Also, it's doubled... Which is fine. Anyway... It would have been nice if ANYWHERE in their documentation it explained this. I had seen references to the .js file. But since I didn't have it, and couldn't find a download for it, I didn't think making the reference in the HTML would work. Plus, they tell you that all you need to do is make the reference in the config.xml file. – durbnpoisn Mar 21 '19 at 11:26
  • I am also quite surprised that this isn't clearly mentioned, it isn't even mentioned in the [default Cordova template](https://github.com/apache/cordova-app-hello-world/blob/master/template_src/www/index.html#L46). It is however mentioned in the [Ionic templates](https://github.com/ionic-team/starters/blob/f123a6ac33f53908e97bce62bf55889689e27659/ionic-angular/base/src/index.html#L18), but that isn't really a logical place to look when troubleshooting Cordova / PhoneGap issues. – Dexter Mar 22 '19 at 08:58
  • Yeah, obviously... I should have thought of that. haha... Update for today - with the edits to my code, I got it working 100% error free. You found the solution. So I don't mind that the bounty was doubled. – durbnpoisn Mar 22 '19 at 12:08