iAd interstitial - close button

1

I'm trying to include a fullscreen iAD in an app, but the advertisement screen is showing up without the close button, the question is whether I have to do it by hand or have any hidden options for it?

I found older things about this, but I do not know if they still reflect the current reality.

This is the base of the code I'm using, but in an "empty" file.

import UIKit
import iAd

class ViewController: UIViewController, ADInterstitialAdDelegate{

    var interstitialAd:ADInterstitialAd!
    var interstitialAdView: UIView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func chamarIAd(sender: AnyObject) {
        loadInterstitialAd()
    }

    func loadInterstitialAd() {
        println("loadInterstitialAd")
        interstitialAd = ADInterstitialAd()
        interstitialAd.delegate = self
    }

    func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
        println("interstitialAdWillLoad")
    }

    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
        println("interstitialAdDidLoad")

        interstitialAdView = UIView()
        interstitialAdView.frame = self.view.bounds
        view.addSubview(interstitialAdView)

        interstitialAd.presentInView(interstitialAdView)
        UIViewController.prepareInterstitialAds()
    }

    func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
        println("interstitialAdActionDidFinish")
        interstitialAdView.removeFromSuperview()
    }

    func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
        println("interstitialAdActionShouldBegin")
        return true
    }

    func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
        println("interstitialAd")
    }

    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
        println("interstitialAdDidUnload")
        interstitialAdView.removeFromSuperview()
    }
}
    
asked by anonymous 05.03.2015 / 04:52

1 answer

1

I think I solved my problem. I'll leave it here for the question to have an answer and in case someone has a better solution.

  • I created the close button inside the viewDidLoad method:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        closeButton = UIButton(frame: CGRect(x: 10, y:  20, width: 20, height: 20))
        closeButton.setBackgroundImage(UIImage(named: "close_button"), forState: UIControlState.Normal)
        closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchUpInside)
    }
    
  • When iAd is being loaded, I add the close button in the view:

    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
        interstitialAdView = UIView()
        interstitialAdView.frame = self.view.bounds
        view.addSubview(interstitialAdView)
    
        interstitialAd.presentInView(interstitialAdView)
        UIViewController.prepareInterstitialAds()
    
        view.addSubview(closeButton)
    }
    
  • I created the button method by removing the iAd and close button from the main view:

    func close() {
        interstitialAdView.removeFromSuperview()
        closeButton.removeFromSuperview()   
    }
    
10.03.2015 / 02:41