6

I am using Swift to implement interstitial ads into my app. I have managed to get it to show interstitial adverts, but there is no close 'X' button to get rid of the advert. Why is this?

user2397282
  • 3,798
  • 15
  • 48
  • 94

3 Answers3

1

I also cannot find answer for why built-in framework does not provide close button but i end up with using following solution.

//iAD interstitial definition with custom placeHolderView and close button
//you do not need to add anything to interface builder. All is done with code

var interstitial:ADInterstitialAd!
var placeHolderView:UIView!
var closeButton:UIButton!

//Full Implemantation of iAD interstitial and delegate methods

 func cycleInterstitial(){
 // create a new interstitial. We set the delegate so that we can be notified 
    interstitial = ADInterstitialAd()
    interstitial.delegate = self;
}

func presentInterlude(){
    // If the interstitial managed to load, then we'll present it now.
    if (interstitial.loaded) {

        placeHolderView = UIView(frame: self.view.frame)
        self.view.addSubview(placeHolderView)

        closeButton = UIButton(frame: CGRect(x: 270, y:  25, width: 25, height: 25))
       //add a cross shaped graphics into your project to use as close button
        closeButton.setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)
        closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown)
        self.view.addSubview(closeButton)


        interstitial.presentInView(placeHolderView)
    }
}

// iAd Delegate Mehtods

// When this method is invoked, the application should remove the view from the screen and tear it down.
// The content will be unloaded shortly after this method is called and no new content will be loaded in that view.
// This may occur either when the user dismisses the interstitial view via the dismiss button or
// if the content in the view has expired.

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!){
    placeHolderView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil

    cycleInterstitial()
}


func interstitialAdActionDidFinish(_interstitialAd: ADInterstitialAd!){
    placeHolderView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil

    println("called just before dismissing - action finished")

}

// This method will be invoked when an error has occurred attempting to get advertisement content.
// The ADError enum lists the possible error codes.
func interstitialAd(interstitialAd: ADInterstitialAd!,
    didFailWithError error: NSError!){
        cycleInterstitial()
}


//Load iAd interstitial
func dislayiAdInterstitial() {
    //iAd interstitial
    presentInterlude()
}


func close() {
    placeHolderView.removeFromSuperview()
    closeButton.removeFromSuperview()
    interstitial = nil

}

cross

bpolat
  • 3,879
  • 20
  • 26
0

using interstitial=InterstitialAd() causes me the same problem as you.

However, if you include the following code in your AppDelegate's application function:

UIViewController.prepareInterstitialAds()

Then, you can generate an interstitial Ad using this:

viewController.requestInterstitialAdPresentation()

I am not sure how to get the callbacks, though.

Myoch
  • 793
  • 1
  • 6
  • 24
0

iOS 9.2.1, Xcode 7.2.1, ARC enabled

Please read my post:

https://stackoverflow.com/a/35467510/4018041

I still use presentFromViewController: method.

Hope this helps! Cheers.

Community
  • 1
  • 1
serge-k
  • 3,394
  • 2
  • 24
  • 55