AdMob 在重定向到 Phonegap 中的新页面后未定义

AdMob is undefined after redirecting to new page in Phonegap

我已将 AdMob Plugin Pro 添加到我的 Phonegap 应用程序中。我按照示例代码将以下代码放入我的应用程序主页:

function onDeviceReady() {
    var admobid = {};
    if( /(android)/i.test(navigator.userAgent) ) {
        admobid = { // for Android
            banner: 'ca-pub-{key hidden}'
            interstitial: 'ca-pub-{key hidden}'
        };
    } else if(/(ipod|iphone|ipad)/i.test(navigator.userAgent)) {
        admobid = { // for iOS
            banner: 'ca-pub-{key hidden}'
            interstitial: 'ca-pub-{key hidden}'
        };
    }
    if(window.AdMob) AdMob.prepareInterstitial( {adId:admobid.interstitial, autoShow:true} );
}
document.addEventListener('deviceready', onDeviceReady, false);

上面的代码可以运行,并且出现了一个插页式广告,但是当我尝试在另一个页面中再次调用 AdMob 时,问题就出现了。我尝试了几种方法,但 none 有效。

以下是我的应用的一些背景信息:

  1. 当用户单击 link 时,我的应用程序将重新加载页面。例如,如果用户在主页中并单击 "setting" 按钮,整个页面将重新加载为“setting.html”
  2. 操作DOM的脚本和内容总是放在<body>
  3. 的最后一行

所以这是我尝试过的:

  1. 复制上面相同的onDeviceReady()addEventListner(),放在另一页。

    • 我发现在第二页中没有再次触发 deviceready 事件。因此当然不会再次显示插页式广告。
  2. 只需将AdMob.prepareInterstitial( {adId:admobid.interstitial, autoShow:true}复制到新页面并在页面加载后执行

    • 我发现 AdMob 变成了 undefined

所以我想知道我是漏掉了什么还是做错了什么?

谢谢!

不要使用多个页面,正如 Cordova 文档所述,单页应用程序是您的朋友。当您加载新页面时,您将失去所有插件和其他 Cordova 功能,除非您在所有页面中包含 cordova.js。

如果您可以重构为 SPA,您的生活将在很多方面变得更轻松。

引用 Cordova docs:

First and foremost - your Cordova applications should adopt the SPA (Single Page Application) design. Loosely defined, a SPA is a client-side application that is run from one request of a web page. The user loads an initial set of resources (HTML, CSS, and JavaScript) and further updates (showing a new view, loading data) is done via AJAX. SPAs are commonly used for more complex client-side applications. GMail is a great example of this. After you load GMail, mail views, editing, and organization are all done by updating the DOM instead of actually leaving the current page to load a completely new one.

Using a SPA can help you organize your application in a more efficient manner, but it also has specific benefits for Cordova applications. A Cordova application must wait for the deviceready event to fire before any plugins may be used. If you do not use a SPA, and your user clicks to go from one page to another, you will have to wait for deviceready to fire again before you make use of a plugin. This is easy to forget as your application gets larger.

Even if you choose not to use Cordova, creating a mobile application without using a single page architecture will have serious performance implications. This is because navigating between pages will require scripts, assets, etc., to be reloaded. Even if these assets are cached, there will still be performance issues.

我是 this cordova-admob plugin 的作者。

作为 ,您最好的机会是迁移到 SPA。也就是说,如果这不可能,也许您可​​以尝试一个丑陋的解决方法,但这在很大程度上取决于您如何实现应用程序:

尝试截获 index.html 中的每个 link 并尝试通过 ajax 加载它。通过 ajax 加载后,您可以加载 index.html 正文中的内容。确保评估新内容中的每个脚本。在这里您可以找到有关我将如何实施的示例:http://plnkr.co/edit/ew21klKYRcfvjX8wrAY1

它是在纯 javascript 中实现的,但如果您未满 jQuery,那么 $(document.body).load(...)

可以让您的生活更轻松