5

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); } }

islander_zero
  • 3,342
  • 3
  • 12
  • 17
  • You can detect close by overriding didDismissScreen in GADInterstitialDelegateAdapter. What you do that that point is where I'm stuck too. I'll let you know if I work it out. – Will Calderwood Oct 23 '14 at 14:21

1 Answers1

6

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());
        }
    });
Will Calderwood
  • 4,393
  • 3
  • 39
  • 64
  • Thanks, touch returns to the game but then I try to call another interstitial, I receive this error: 2014-10-24 01:25:24.938 IOSLauncher[7680:2519837] Request Error: Will not send request because interstitial object has been used. Error Domain=com.google.ads Code=6 "Request Error: Will not send request because interstitial object has been used." UserInfo=0x23e02490 {NSLocalizedDescription=Request Error: Will not send request because interstitial object has been used., NSLocalizedFailureReason=Request Error: Will not send request because interstitial object has been used.} – islander_zero Oct 23 '14 at 15:27
  • This should really be a different question. Check interstitial.isBeenUsed(). If it returns true then re-create the instance of interstitial and set the ad unit ID again. Please accept the answer if this solved your problem. Thanks. – Will Calderwood Oct 23 '14 at 15:31
  • Ideally you want to load a new instance of the ad in didDismissScreen so it's ready for next time you need it. – Will Calderwood Oct 23 '14 at 15:32
  • You're right @Will CalderWood.. it works perfectly now. I just called my whole intializeInterstitial() within the didDimissScreen(). Hope this won't be an issue but it's working fine... thanks again! – islander_zero Oct 23 '14 at 15:46