Advertisement
bytejourney

IOSLauncher - Admob Interstitial iOS libGDX Project

Sep 29th, 2022
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. package com.bytejourney.ios.interstitial;
  2.  
  3. import static org.robovm.pods.google.mobileads.GADRequest.GADSimulatorID;
  4.  
  5. import org.robovm.apple.foundation.NSArray;
  6. import org.robovm.apple.foundation.NSAutoreleasePool;
  7. import org.robovm.apple.foundation.NSError;
  8. import org.robovm.apple.uikit.UIApplication;
  9. import org.robovm.objc.block.VoidBlock2;
  10. import org.robovm.pods.google.mobileads.GADFullScreenContentDelegateAdapter;
  11. import org.robovm.pods.google.mobileads.GADFullScreenPresentingAd;
  12. import org.robovm.pods.google.mobileads.GADInterstitialAd;
  13. import org.robovm.pods.google.mobileads.GADMobileAds;
  14. import org.robovm.pods.google.mobileads.GADRequest;
  15.  
  16. import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
  17. import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
  18. import com.bytejourney.ios.interstitial.MyGdxGame;
  19.  
  20. public class IOSLauncher extends IOSApplication.Delegate implements AdsController  {
  21.     private IOSApplication iosApplication;
  22.  
  23.     @Override
  24.     protected IOSApplication createApplication() {
  25.         IOSApplicationConfiguration config = new IOSApplicationConfiguration();
  26.  
  27.         GADMobileAds.sharedInstance().getRequestConfiguration().setTestDeviceIdentifiers(new NSArray<>(GADSimulatorID()));
  28.         GADMobileAds.sharedInstance().start(status -> {
  29.             System.out.println("GADMobileAds started with status == " + status);
  30.         });
  31.  
  32.         iosApplication = new IOSApplication(new MyGdxGame(this), config);
  33.         return iosApplication;
  34.     }
  35.  
  36.     public static void main(String[] argv) {
  37.         NSAutoreleasePool pool = new NSAutoreleasePool();
  38.         UIApplication.main(argv, null, IOSLauncher.class);
  39.         pool.close();
  40.     }
  41.  
  42.     @Override
  43.     public void loadInterstitial() {
  44.         String unitId = "ca-app-pub-3940256099942544/1033173712"; // test id admob
  45.         GADRequest request = new GADRequest();
  46.  
  47.         GADInterstitialAd.load(unitId, request, new VoidBlock2<GADInterstitialAd, NSError>() {
  48.  
  49.             @Override
  50.             public void invoke(GADInterstitialAd ad, NSError error) {
  51.  
  52.                 if (error != null) {
  53.                     System.out.println("Failed to load ad due  " + error);
  54.                 } else {
  55.                     ad.setFullScreenContentDelegate(new GADFullScreenContentDelegateAdapter(){
  56.                         @Override
  57.                         public void adDidDismissFullScreenContent(GADFullScreenPresentingAd ad) {
  58.                             super.adDidDismissFullScreenContent(ad);
  59.                             System.out.println("adDidDismissFullScreenContent");
  60.                         }
  61.  
  62.                         @Override
  63.                         public void didFailToPresentFullScreenContent(GADFullScreenPresentingAd ad, NSError error) {
  64.                             System.out.println("didFailToPresentFullScreenContent  " + error);
  65.                         }
  66.                     });
  67.                     ad.presentFromRootViewController(iosApplication.getUIViewController()); // show the interstitial ad
  68.                 }
  69.             }
  70.         });
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement