Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bytejourney.ios.banner;
- import static org.robovm.pods.google.mobileads.GADRequest.GADSimulatorID;
- import org.robovm.apple.coregraphics.CGRect;
- import org.robovm.apple.coregraphics.CGSize;
- import org.robovm.apple.foundation.NSArray;
- import org.robovm.apple.foundation.NSAutoreleasePool;
- import org.robovm.apple.foundation.NSError;
- import org.robovm.apple.uikit.UIApplication;
- 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.GADBannerViewDelegateAdapter;
- import org.robovm.pods.google.mobileads.GADMobileAds;
- import org.robovm.pods.google.mobileads.GADRequest;
- import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
- import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
- public class IOSLauncher extends IOSApplication.Delegate implements AdsController {
- private static GADBannerView bannerView;
- private IOSApplication iosApplication;
- private float bannerHeight;
- private double screenWidth;
- private double screenHeight;
- private double adWidth;
- private double adHeight;
- private float bannerWidth;
- @Override
- protected IOSApplication createApplication() {
- IOSApplicationConfiguration config = new IOSApplicationConfiguration();
- GADMobileAds.sharedInstance().getRequestConfiguration().setTestDeviceIdentifiers(new NSArray<>(GADSimulatorID()));
- GADMobileAds.sharedInstance().start(status -> {
- System.out.println("GADMobileAds started with status == " + status);
- });
- iosApplication = new IOSApplication(new MyGdxGame(this), config);
- return iosApplication;
- }
- public static void main(String[] argv) {
- NSAutoreleasePool pool = new NSAutoreleasePool();
- UIApplication.main(argv, null, IOSLauncher.class);
- pool.close();
- }
- @Override
- public void loadBanner() {
- if (bannerView == null) {
- CGRect cgRect = iosApplication.getUIViewController().getView().getFrame();
- cgRect.setY(cgRect.getHeight());
- bannerView = new GADBannerView(GADAdSize.Banner());
- GADAdSize adSize = getAdSize();
- bannerView.setAdSize(adSize);
- bannerView.setAdUnitID("ca-app-pub-3940256099942544/6300978111");
- bannerView.setRootViewController(iosApplication.getUIViewController());
- iosApplication.getUIViewController().getView().addSubview(bannerView);
- }
- final GADRequest request = GADRequest.request();
- bannerView.setDelegate(new GADBannerViewDelegateAdapter() {
- @Override
- public void didReceiveAd(GADBannerView view) {
- super.didReceiveAd(view);
- System.out.println("didReceiveAd.");
- }
- @Override
- public void didFailToReceiveAd(GADBannerView bannerView, NSError error) {
- super.didFailToReceiveAd(bannerView, error);
- System.out.println("didFailToReceiveAd.");
- }
- });
- bannerView.loadRequest(request);
- bannerView.setFrame(new CGRect((screenWidth / 2) - bannerWidth / 2,
- 0 + iosApplication.getUIViewController().getView().getSafeAreaInsets().getTop(), bannerWidth, bannerHeight)); // top banner ad
- // screenHeight - bannerHeight, bannerWidth, bannerHeight)); // bottom banner ad
- }
- public GADAdSize getAdSize(){
- // Determine the screen width (less decorations) to use for the ad width.
- final CGSize screenSize = UIScreen.getMainScreen().getBounds().getSize();
- screenWidth = screenSize.getWidth();
- screenHeight = screenSize.getHeight();
- final CGSize adSize = bannerView.getBounds().getSize();
- adWidth = adSize.getWidth();
- adHeight = adSize.getHeight();
- bannerWidth = (float) screenWidth;
- bannerHeight = (float) (bannerWidth / adWidth * adHeight);
- return GADAdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(bannerWidth);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment