I have a problem with interstitial ads from Apple. I'm doing my first app in swift, which I want to put on App store, as soon as it will be possible. But when I rewrite code for interstitial ads from obejctive-c to swift, I am able to show up the ad, but i does not have X close buuton, so I can not close it. I have not found anywher, taht I should place this button on my own, it should be there by default. I am using this functions: (I also have to say, that I'm using sprite kit, so my view controller automaticaly redirects game to Gamescene)
func showFullScreenAd() {
if requestingAd == false {
interstitial = ADInterstitialAd()
interstitial!.delegate = self
requestingAd = true
}
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
self.interstitial = nil
requestingAd = false
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
if interstitialAd != nil && self.interstitial != nil && self.requestingAd == true {
interstitial!.presentInView(self.view)
}
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
interstitial = nil
requestingAd = false
}
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
interstitial = nil
requestingAd = false
}
Thank you for help (here is a picture: http://imgur.com/jKTWRiG)
-----UPDATE----- This is solution, which just works. I make UIView, in which I present ad and close button (some cross image on background). This may not be the best solution, but app store accepted that, soooo :D
All these funcs are placed in GameViewController.swift (you can't simply control view controller from spritekit scene, you could through singleton of gameviewcontroller, it is up to you)
func showFullScreenAd() {
if requestingAd == false {
interstitial = ADInterstitialAd()
interstitial!.delegate = self
requestingAd = true
}
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
self.interstitial = nil
requestingAd = false
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
if interstitialAd != nil && _interstitial != nil && requestingAd == true {
self._adView = UIView()
self._adView!.frame = self.view.bounds
self.view.addSubview(_adView!)
self.button = UIButton(frame: CGRect(x: 10, y: 10, width: 40, height: 40))
self.button!.setBackgroundImage(UIImage(named: "close_button"), forState: UIControlState.Normal)
self.button!.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(button!)
_interstitial!.presentInView(self._adView)
requestingAd = false
}
UIViewController.prepareInterstitialAds()
}
func close() {
self._adView!.removeFromSuperview()
self.button!.removeFromSuperview()
self._interstitial = nil
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
interstitial = nil
requestingAd = false
self._adView!.removeFromSuperview()
self.button!.removeFromSuperview()
}
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
interstitial = nil
requestingAd = false
self._adView!.removeFromSuperview()
self.button!.removeFromSuperview()
}
iOS 9.2.1, Xcode 7.2.1, ARC enabled
I am using the old method:
[interstitial presentFromViewController:self];
Otherwise, the status bar is still shown using a container view to present in view and there is no "X" button, or you get no call backs using the suggested methods by Apple, i.e:
[self requestInterstitialAdPresentation];
[interstitial presentInView:self.view];
You can suppress the warnings if it bothers you:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
//method goes here
#pragma clang diagnostic pop
Please read this post: iOS 7 iAd interstitial ads can not be closed by user
What I do is enclose the presentation in a method like so, add some polish with some conditions:
*.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface MainViewController : UIViewController <ADInterstitialAdDelegate>
{
ADInterstitialAd *interstitial;
}
@end
*.m
- (void)viewDidLoad
{
[super viewDidLoad];
[UIViewController prepareInterstitialAds];
self.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
interstitial = [[ADInterstitialAd alloc] init];
interstitial.delegate = self;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)presentInterlude
{
if (interstitial.loaded && [self shouldPresentInterstitialAd])
{
[interstitial presentFromViewController:self];
}
else
{
nil;
}
}
#pragma clang diagnostic pop
- (void)cycleInterstitial
{
interstitial.delegate = nil;
interstitial = [[ADInterstitialAd alloc] init];
interstitial.delegate = self;
}
- (void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd
{
NSLog(@"loaded");
nil;
}
- (void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd
{
NSLog(@"unloaded");
[self cycleInterstitial];
}
- (void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error
{
NSLog(@"failed, error: %@", error);
[self cycleInterstitial];
}
- (void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd
{
NSLog(@"finish");
}
- (BOOL)interstitialAdActionShouldBegin:(ADInterstitialAd *)interstitialAd willLeaveApplication:(BOOL)willLeave
{
NSLog(@"action");
return true;
}
@end
Then use [self presentInterlude]
when you are ready to present the interstitial ad.
To tell when the ad is displayed and when the ad is closed using the "X" button (without the user clicking the ad content), you must take advantage of the fact that when the ad is displayed viewDidDissapear:
is called, and when the ad is closed viewDidAppear:
is called. If you have some content that needs to be ready before viewDidAppear:
is called, then use the viewWillAppear:
method.
Don't forget to cycle the interstitial after each presentation, the interstitial ad object gets released after presentation, which is the biggest difference between an interstitial ad and a banner type ad.
If someone has a better solution, then please share and post!
Thanks! Cheers.
来源:https://stackoverflow.com/questions/25285344/interstitialad-ios-8-beta-5-does-not-provide-x-close-button-in-simulator