Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect close of Interstitial Ad in AdMob using iOS RoboVM/libgdx

I'm using RoboVM bindings for my iOS application to display AdMob interstitials. When I close the interstitial ad, I lose all touch controls. Is there a way to detect the ad being closed so I can put the touch back to the game? Or is there a better way to implement interstitials? Here's my code below:

public class IOSLauncher extends IOSApplication.Delegate implements IActivityRequestHandler{
private static final Logger log = new Logger(IOSLauncher.class.getName(), Application.LOG_DEBUG);
private IOSApplication iosApplication;

//interstitial
private static final String INTERSTITIAL_AD = "MY_AD_ID";
private GADInterstitial interstitial;
private UIWindow window;
private UIViewController rootViewController;

@Override
protected IOSApplication createApplication() {
    IOSApplicationConfiguration config = new IOSApplicationConfiguration();
    config.orientationLandscape = true;
    config.orientationPortrait = false;

    iosApplication = new IOSApplication(new PaperPig(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 initializeAds() {
    intializeInterstitial();
}

public void intializeInterstitial () {
    rootViewController = new UIViewController();

    interstitial = new GADInterstitial();
    interstitial.setAdUnitID(INTERSTITIAL_AD);

    interstitial.setDelegate(new GADInterstitialDelegateAdapter() {
        @Override
        public void didReceiveAd (GADInterstitial ad) {
            System.out.println("Did receive ad.");
        }

        @Override
        public void didFailToReceiveAd (GADInterstitial ad, GADRequestError error) {
            System.out.println(error.description());
            System.out.println(error.getErrorCode());
        }
    });

    window = new UIWindow(UIScreen.getMainScreen().getBounds());
    window.setRootViewController(rootViewController);
    window.addSubview(rootViewController.getView());

    interstitial.loadRequest(GADRequest.create());
}

@Override
public void showOrLoadInterstital() {
    if (interstitial.isReady()) {
        if (rootViewController == null) {
            rootViewController = new UIViewController();
        }
        if (window == null) {
            window = new UIWindow(UIScreen.getMainScreen().getBounds());
            window.setRootViewController(rootViewController);
        }
        window.makeKeyAndVisible();
        interstitial.present(rootViewController);
    }

//Return touch back to Game
//UIApplication.getSharedApplication().getKeyWindow().setRootViewController(rootViewController); } }

like image 861
islander_zero Avatar asked Oct 23 '14 13:10

islander_zero


1 Answers

You need to call:

        window.setHidden(true);

Change your creation of GADInterstitialDelegateAdapter() to the following

   interstitial.setDelegate(new GADInterstitialDelegateAdapter() {
        @Override
        public void didReceiveAd (GADInterstitial ad) {
            System.out.println("Did receive ad.");

        }

        @Override
        public void didDismissScreen(GADInterstitial ad) {
             window.setHidden(true);
        }

        @Override
        public void didFailToReceiveAd (GADInterstitial ad, GADRequestError error) {
            System.out.println(error.description());
            System.out.println(error.getErrorCode());
        }
    });
like image 145
Will Calderwood Avatar answered Sep 28 '22 06:09

Will Calderwood