LibGDX and google-mobile-ads Robopods integration (iOS)

I just started porting my existing android game (word-hunter) to iOS and ran into a lot of troubles trying to make adMob works on iOS with libgdx and robovm, so I figure someone else might need this :)

You should probably already have an Interface in your libgdx core project to specify the actions correspond to the display of ads:

public interface AdsDisplayInterface{
   public void loadAd();
   public void showAd();
}

And your game constructor should expect an implementation of this interface as argument:

public class Game implements ApplicationListener {
   ...
   public AdsDisplayInterface _adsDisplay;

   public Game(AdsDisplayInterface adsDisplay){
      _adsDisplay = adsDisplay;
   }

   public Game(){
      ...
   }

   @Override
      public void create() {
      ...
   }
}

Now you can call _adsDisplay.loadAd() and _adsDisplay.showAd() anytime you want in your game, see this post for more details on how this works.

Now we want to create a class implementing this Interface in the platform where we want to display ads, in our case: iOS.

First we need to integrate the google-mobile-ads robopod binding (git repo).

Follow the instructions from the README in the git website and use either graddle or maven to add the binding.

I added compile “org.robovm:robopods-google-mobile-ads-ios:$roboVMVersion” in the dependencies of the project(“:ios”) in the main build.gradle of my libgdx projects, used ./gradlew eclipse and then imported the projects into Eclipse.

Another option if you don’t want to use graddle or maven is to directly grab the jar files you need (robopods-google-mobile-ads-ios-$robovmVersion.jar and robopods-google-apis-ios$robovmVersion.jar) from the robovm maven directory and add then to your ios project /lib folder (create it if necessary) and add both jar to the build bath.

Now we need to create an implementation of the AdsDisplayInterface in our ios project:

@CustomClass("ViewController")
public class ViewController implements AdsDisplayInterface {
 private GADInterstitial interstitial;

 private GADInterstitial createAndLoadInterstitial() {
     //Ad Unit ID of your interstital, from your adMob account. Use the TEST one for now
     GADInterstitial interstitial = new GADInterstitial("ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx");
     interstitial.setDelegate(new GADInterstitialDelegateAdapter() {
         @Override
         public void didDismissScreen(GADInterstitial ad) {
             ViewController.this.interstitial = createAndLoadInterstitial();
         }
     });
     interstitial.loadRequest(createRequest());
     return interstitial;
 }

 private GADRequest createRequest() {
     GADRequest request = new GADRequest();
     // To test on your devices, add their UDIDs here:
     request.setTestDevices(Arrays.asList(GADRequest.getSimulatorID()));
     return request;
 }

 @Override
 public void loadAds() {
     interstitial = createAndLoadInterstitial();
 }

 @Override
 public void showAds() {
     if (interstitial.isReady()) {
         interstitial.present(UIApplication.getSharedApplication().getKeyWindow().getRootViewController());
     } else {
         System.out.println("Interstitial not ready!");
     }
 }

}

I used this sample application and modified a couple of things to make it work with libgdx. If you need to add banners, check the sample and it should be quite straightforward.

Now we need to modify our IOSLauncher in the libgdx IOS application:

public class IOSLauncher extends IOSApplication.Delegate {

 private GADInterstitial interstitial;

 @Override
 protected IOSApplication createApplication() {
     Words.setPlatformResolver(new DesktopResolver());

     IOSApplicationConfiguration config = new IOSApplicationConfiguration();
     return new IOSApplication(new Game(new ViewController()), config);
 }

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

And now it should work :) Your IOS ad will show up whenever you call loadAd() then showAd() in your game.

Hopefully I didn’t forget any steps, otherwise let me know !

Advertisements

14 thoughts on “LibGDX and google-mobile-ads Robopods integration (iOS)

  1. Hi I am trying to replicate this in my project but even when I copy that viewcontroller from github i still get a compile error. Could you please share the viewcontroller you used?

  2. In your example is the ViewController class an actual ViewController – i.e. does it interact with the Interface Builder layout in some way? If so, is there anything special we have to do in Interface Builder?

  3. Thanks so much for this! It helped me a lot.
    I dropped the arguments out of the loadAd() showAd() calls as you declared the methods without them? Also, I setup an dummy implementation of the AdsDisplayInterface to keep the Game class constructor happy when testing on the desktop as no interface impl. is received from the ios launcher in this case.
    One question, I take it this will call live ads when the app is published through the app store, rather than the test ads?
    Thanks again, good work!
    Andrew

    • Hello,
      If you want to show live ads then you need to replace the ads ID in the example by the one provided by google adMob when you create you application there.
      Don’t forget to do it, before building for publishing, it won’t be done automatically :)

      I have removed the useless arguments from the sample, thanks for that.

  4. Hi upandcrawling, thank you for this very nice article, it helps a lots.
    I faced a problem when importing libraries. Everything looks ok but Android Studio doesn’t recognize “org.robovm.pods” but everything is ok on gradle file’s side. Did you face it too ?

    • Hi,

      I don’t use android studio but I think you need to launch a gradle build either in Android studio if a gradle plugin exist or in cmd line outside to get the libs on your computer.

      Good luck :)

    • you have to download robovm plugin first and add it to AndroidStudio. Unfortunately your wont be able to get it at their official site get it from github and add it manually. it will work…i did the same…

  5. I am doing the same but i am getting an error and i m getting nuts seeing it again and again.
    the error says

    java.lang.NoClassDefFoundError: org/robovm/apple/foundation/NSObject$Handle
    at org.robovm.pods.google.mobileads.GADRequest.(GADRequest.java)
    at com.xxxxxxxx.ViewController.createRequest(ViewController.java)
    at com.xxxxxxxx.ViewController.createAndLoadBanner(ViewController.java)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s