bytejourney

IOSLauncher - Admob Banner iOS libGDX Project

Sep 29th, 2022 (edited)
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. package com.bytejourney.ios.banner;
  2.  
  3. import static org.robovm.pods.google.mobileads.GADRequest.GADSimulatorID;
  4.  
  5. import org.robovm.apple.coregraphics.CGRect;
  6. import org.robovm.apple.coregraphics.CGSize;
  7. import org.robovm.apple.foundation.NSArray;
  8. import org.robovm.apple.foundation.NSAutoreleasePool;
  9. import org.robovm.apple.foundation.NSError;
  10. import org.robovm.apple.uikit.UIApplication;
  11. import org.robovm.apple.uikit.UIScreen;
  12. import org.robovm.pods.google.mobileads.GADAdSize;
  13. import org.robovm.pods.google.mobileads.GADBannerView;
  14. import org.robovm.pods.google.mobileads.GADBannerViewDelegateAdapter;
  15. import org.robovm.pods.google.mobileads.GADMobileAds;
  16. import org.robovm.pods.google.mobileads.GADRequest;
  17.  
  18. import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
  19. import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
  20.  
  21. public class IOSLauncher extends IOSApplication.Delegate  implements AdsController  {
  22.     private static GADBannerView bannerView;
  23.     private IOSApplication iosApplication;
  24.     private float bannerHeight;
  25.     private double screenWidth;
  26.     private double screenHeight;
  27.     private double adWidth;
  28.     private double adHeight;
  29.     private float bannerWidth;
  30.     @Override
  31.     protected IOSApplication createApplication() {
  32.         IOSApplicationConfiguration config = new IOSApplicationConfiguration();
  33.  
  34.         GADMobileAds.sharedInstance().getRequestConfiguration().setTestDeviceIdentifiers(new NSArray<>(GADSimulatorID()));
  35.         GADMobileAds.sharedInstance().start(status -> {
  36.             System.out.println("GADMobileAds started with status == " + status);
  37.         });
  38.  
  39.         iosApplication = new IOSApplication(new MyGdxGame(this), config);
  40.         return iosApplication;
  41.  
  42.     }
  43.  
  44.     public static void main(String[] argv) {
  45.         NSAutoreleasePool pool = new NSAutoreleasePool();
  46.         UIApplication.main(argv, null, IOSLauncher.class);
  47.         pool.close();
  48.     }
  49.  
  50.     @Override
  51.     public void loadBanner() {
  52.         if (bannerView == null) {
  53.             CGRect cgRect = iosApplication.getUIViewController().getView().getFrame();
  54.             cgRect.setY(cgRect.getHeight());
  55.  
  56.             bannerView = new GADBannerView(GADAdSize.Banner());
  57.             GADAdSize adSize = getAdSize();
  58.             bannerView.setAdSize(adSize);
  59.             bannerView.setAdUnitID("ca-app-pub-3940256099942544/6300978111");
  60.             bannerView.setRootViewController(iosApplication.getUIViewController());
  61.             iosApplication.getUIViewController().getView().addSubview(bannerView);
  62.         }
  63.  
  64.         final GADRequest request = GADRequest.request();
  65.  
  66.         bannerView.setDelegate(new GADBannerViewDelegateAdapter() {
  67.             @Override
  68.             public void didReceiveAd(GADBannerView view) {
  69.                 super.didReceiveAd(view);
  70.                 System.out.println("didReceiveAd.");
  71.             }
  72.  
  73.             @Override
  74.             public void didFailToReceiveAd(GADBannerView bannerView, NSError error) {
  75.                 super.didFailToReceiveAd(bannerView, error);
  76.                 System.out.println("didFailToReceiveAd.");
  77.             }
  78.         });
  79.  
  80.         bannerView.loadRequest(request);
  81.         bannerView.setFrame(new CGRect((screenWidth / 2) - bannerWidth / 2,
  82.                 0 + iosApplication.getUIViewController().getView().getSafeAreaInsets().getTop(), bannerWidth, bannerHeight)); // top banner ad
  83. //                screenHeight - bannerHeight, bannerWidth, bannerHeight)); // bottom banner ad
  84.     }
  85.     public GADAdSize getAdSize(){
  86.         // Determine the screen width (less decorations) to use for the ad width.
  87.         final CGSize screenSize = UIScreen.getMainScreen().getBounds().getSize();
  88.         screenWidth = screenSize.getWidth();
  89.         screenHeight = screenSize.getHeight();
  90.  
  91.         final CGSize adSize = bannerView.getBounds().getSize();
  92.         adWidth = adSize.getWidth();
  93.         adHeight = adSize.getHeight();
  94.  
  95.         bannerWidth = (float) screenWidth;
  96.         bannerHeight = (float) (bannerWidth / adWidth * adHeight);
  97.         return GADAdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(bannerWidth);
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment