Skip to content

Instantly share code, notes, and snippets.

@brthornbury
Created February 26, 2014 20:21
Show Gist options
  • Save brthornbury/9237686 to your computer and use it in GitHub Desktop.
Save brthornbury/9237686 to your computer and use it in GitHub Desktop.
Stick this in the RobovmLauncher class created by the libgdx project ui for mopub ads. This is interacting with blueriver's robovm mopub bindings. You can use this to set up admob with libgdx on iOS.
@Override
public boolean didFinishLaunching (UIApplication application, NSDictionary options) {
super.didFinishLaunching(application,options);
// We need a view controller to see ads.
//rootViewController = ((IOSApplication) Gdx.app).getUIViewController();
// If you are already using a UIWindow with a root view controller, get the root view controller (f.e. LibGDX):
rootViewController = ((IOSApplication)Gdx.app).getUIViewController();
// Create a MoPub ad. In this case a banner, but you can make it any size you want.
final MPAdView banner = new MPAdView(BANNER_AD_UNIT_ID, MPConstants.MOPUB_BANNER_SIZE);
// Let's calculate our banner size. We need to do this because the resolution of a retina and normal device is different.
float bannerWidth = UIScreen.getMainScreen().getBounds().size().width();
float bannerHeight = bannerWidth / MPConstants.MOPUB_BANNER_SIZE.width() * MPConstants.MOPUB_BANNER_SIZE.height();
System.out.println(bannerWidth);
System.out.println(bannerHeight);
// Let's set the frame (bounds) of our banner view. Remember on iOS view coordinates have their origin top-left.
// Position banner on the top.
// banner.setFrame(new CGRect(0, 0, bannerWidth, bannerHeight));
// Position banner on the bottom.
banner.setFrame(new CGRect(0, UIScreen.getMainScreen().getBounds().size().height() - bannerHeight, bannerWidth,
bannerHeight));
// Let's color the background for testing, so we can see if it is positioned correctly, even if no ad is loaded yet.
//banner.setBackgroundColor(new UIColor(1, 0, 0, 1)); // Remove this after testing.
// The delegate for the banner. It is required to override getViewController() to get ads.
// Add banner to our view controller.
// // We add the ad view controller to our root view controller.
adViewController = new MPAdViewController(banner);
// The delegate for the banner. It is required to override getViewController() to get ads.
MPAdViewDelegate bannerDelegate = new MPAdViewDelegate.Adapter() {
@Override
public UIViewController getViewController () {
return adViewController;
}
};
banner.setDelegate(bannerDelegate);
// Add banner to our view controller.
adViewController.getView().addSubview(banner);
// We add the ad view controller to our root view controller.
rootViewController.addChildViewController(adViewController);
rootViewController.getView().addSubview(adViewController.getView());
banner.loadAd();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment