在我的iOS libgdx游戏中实现AdMob?

时间:2015-09-14 20:03:53

标签: ios android-studio libgdx admob robovm

据我所知,上面描述的方法是将以下代码行添加到gradle中

project(":ios") {
    apply plugin: "java"
    apply plugin: "robovm"

    dependencies {
        compile project(":core")
        //...
        compile "org.robovm:robopods-google-mobile-ads-ios:1.6.0"
    }
}

然而,在这样做之后,我迷失了下一步是什么..我期待类似于我所做的一些东西,使它与Android设备一起工作。但是,我似乎无法找到正确的信息,对于我的下一步应该是非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

添加该行后,右键单击您的iOS项目和Gradle>全部刷新。

您现在拥有等级依赖的robopods jar。

对于你的iOSLauncher.java:

package com.YOURPACKAGENAME;//TODO


import org.robovm.apple.coregraphics.CGRect;
import org.robovm.apple.coregraphics.CGSize;
import org.robovm.apple.foundation.Foundation;
import org.robovm.apple.foundation.NSAutoreleasePool;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIApplicationLaunchOptions;
import org.robovm.apple.uikit.UIScreen;
import org.robovm.pods.google.mobileads.GADAdSize;
import org.robovm.pods.google.mobileads.GADBannerView;
import org.robovm.pods.google.mobileads.GADRequest;

import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
import com.YOURPACKAGENAME.GAMECLASS;//TODO
import com.badlogic.gdx.Application;
import com.badlogic.gdx.utils.Logger;

import org.robovm.pods.google.mobileads.GADBannerViewDelegateAdapter;
import org.robovm.pods.google.mobileads.GADRequestError;

public class IOSLauncher extends IOSApplication.Delegate {

    private static CGSize AD_SIZE;
    private static final Logger log = new Logger(IOSLauncher.class.getName(), Application.LOG_DEBUG);
    private GADBannerView adview;
    private boolean adsInitialized = false;
    private IOSApplication iosApplication;

    @Override
    protected IOSApplication createApplication() {
        GAMECLASS app = new GAMECLASS();//TODO
        IOSApplicationConfiguration config = new IOSApplicationConfiguration();
        config.orientationLandscape = false; 
        config.orientationPortrait = true; 
        iosApplication = new IOSApplication(app, config);
        return iosApplication;
    }

    public static void main(String[] argv) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(argv, null, IOSLauncher.class);
        pool.close();
    }

    // Ads
    public void showAd() {
        initializeAds();
        //Portrait bottom screen
        final CGSize screenSize = UIScreen.getMainScreen().getBounds().getSize();
        double screenWidth = screenSize.getWidth();
        AD_SIZE = adview.getBounds().getSize();
        double adWidth = AD_SIZE.getWidth();
        double adHeight = AD_SIZE.getHeight();
        float bannerWidth = (float) screenWidth;
        float bannerHeight = (float) (bannerWidth / adWidth * adHeight);
        double screenHeight = screenSize.getHeight();
        double adX = (screenWidth / 2) - (adWidth / 2);
        double adY = screenHeight - bannerHeight;

        //Landscape top screen
        //<string>UIInterfaceOrientationLandscapeRight</string>
        //<string>UIInterfaceOrientationLandscapeLeft</string>
        /*
        double adWidth = adSize.getWidth();
        double adHeight = adSize.getHeight();
        double screenHeight = screenSize.getHeight();
        double screenWidth = screenSize.getWidth();
        float bannerWidth = (float) screenWidth/2;
        float bannerHeight = (float) ((float) screenHeight/10.0);
        double adX = (screenWidth / 2) - (adWidth / 2);
        double adY = 0;
        */

        adview.setFrame(new CGRect(adX, adY, bannerWidth, bannerHeight));


    }
    @Override
    public boolean didFinishLaunching (UIApplication application, UIApplicationLaunchOptions launchOptions) {
        boolean didFinish = super.didFinishLaunching(application, launchOptions);

        this.showAd();

        return didFinish;
    }

    public void initializeAds() {

        if (!adsInitialized) {
            Foundation.log("Initalizing ads...");

            adsInitialized = true;


            adview = new GADBannerView(GADAdSize.SmartBannerPortrait());
            adview.setAdUnitID("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); //put your secret key here TODO
            adview.setRootViewController(iosApplication.getUIViewController());
            iosApplication.getUIViewController().getView().addSubview(adview);


            final GADRequest request = new GADRequest();

            adview.setDelegate(new GADBannerViewDelegateAdapter() {
                @Override
                public void didReceiveAd(GADBannerView view) {
                    super.didReceiveAd(view);
                    log.debug("didReceiveAd");
                }

                @Override
                public void didFailToReceiveAd(GADBannerView view,
                                               GADRequestError error) {
                    super.didFailToReceiveAd(view, error);
                    //log.debug("didFailToReceiveAd:" + error);
                    Foundation.log("ERROR at didFailToReceiveAd: " + error);
                }
            });

            adview.loadRequest(request);
            Foundation.log("Initalizing ads complete.");
        }
    }
}

适用于iOS 9。 此外,如果您在运行时遇到任何问题,请确保在最新更新后清理roboVM缓存。