问题
We show interstitial iAds between levels. Since iAds appear to require quite a bit of memory (we used to receive many memory warnings when they are shown and experienced some crashes), we do not load the new view before the interstitial has been closed (so, we first unload the game view, then we show the interstitial, then we load the game view).
For this, we used the old/deprecated way of showing interstitials:
allocate ADInterstitialAd and set delegate
_interstitial = [[ADInterstitialAd alloc] init]; _interstitial.delegate = self;
when ready, present Ad in some viewcontroller:
[_interstitial presentFromViewController:_rootViewController];
listen to delegate methods to detect when the user closed the interstitial ad:
- (void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd { [self proceedToNextLevel]; }
This used to work in iOS 7. In iOS8 however, while most of the delegate functions get called, interstitialAdActionDidFinish does not get called (interstitialAdDidUnload does get called, but only like 5 minutes later).
So, there seems to be some new way of showing interstitial ads through a category on UIViewController: https://developer.apple.com/library/ios/documentation/iAd/Reference/UIViewController_iAd_Additions/index.html#//apple_ref/occ/instm/UIViewController/shouldPresentInterstitialAd
So the new way would be:
prepare ads through static method call:
[UIViewController prepareInterstitialAds];
when ready, request display of ad:
_rootViewController.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual; [_rootViewController requestInterstitialAdPresentation];
This does show the interstitial - however, since there is no delegate anymore, there is no way of telling when the user closed the interstitial.
So the question is: How on iOS 8 (and also compatible to iOS 7) can we tell when a user closed an interstitial Ad?
//edit: I also tried to query
viewController.isPresentingFullScreenAd
repeatedly with a timer, but while this works on iOS 7, on iOS 8 the property is always returning true, even after the user closed the interstitial ad
回答1:
When an interstitial iAd closes, viewDidAppear is called in your VC.
Here's how I implement...
I call the following code from my SKScene when I want to show an ad.
MyScene.m
if ([viewController showIAd]) // attempting to show an ad
{
NSLog(@"showIAd returned YES, ad shown");
// Do nothing. Wait for viewDidAppear to be called.
}
else
{
// No ad was ready, do what needs to happen after ad.
}
When an iAd is displayed I set a BOOL called currentlyShowingAnIAd. When viewDidAppear runs it knows it's returning from an ad.
viewController.m
-(void)viewDidAppear:(BOOL)animated
{
NSLog(@"viewDidAppear");
if (self.currentlyShowingAnIAd)
{
self.currentlyShowingAnIAd = NO;
// Do what needs to happen after ad.
}
}
-(BOOL)showIAd
{
NSLog(@"showIAd method run");
self.currentlyShowingAnIAd = [self requestInterstitialAdPresentation];
return self.currentlyShowingAnIAd;
}
Hope this helps :)
来源:https://stackoverflow.com/questions/27232008/interstitial-iad-how-to-detect-closing-on-ios-8