iAds interstitial adverts on iPhone?

前端 未结 2 1610
我在风中等你
我在风中等你 2020-11-29 02:00

My developer and I had previously tried to get interstitial adverts loading on iPhone and iPad, however only managed to get this working on iPad.

During our testing

2条回答
  •  甜味超标
    2020-11-29 02:34

    My original answer is starting to age away so here's a newer Swift implementation:

    This implementation uses a Manual ADInterstitialPresentationPolicy so we can present our interstitials at our own intervals. When manually presenting your interstitial it does not load with its own close button to dismiss itself. So, what I've done is created a UIView to present the interstitial in and used the interstitial's delegate methods to dismiss the UIView. The inconsistency with receiving an ad from the iAd network still arises when testing. Sometimes you receive an ad, sometimes it fails to load which allows us to request a new ad, and sometimes nothing happens at all. This just seems to be the nature of iAd's interstitials.

    import UIKit
    import iAd // Import iAd
    
    class ViewController: UIViewController, ADInterstitialAdDelegate { // Include the delegate
    
        var iAdInterstitial = ADInterstitialAd() // Our ad
        var iAdInterstitialView = UIView() // View to present our ad in
        var adLoaded = false // Bool to keep track if an ad is loaded or not
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupAd()
        }
    
        func setupAd() {
            // Set presentation to manual so we can choose when to present the interstitial
            // Setting this will also fetch an interstitial ad for us
            self.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Manual
            iAdInterstitial.delegate = self // Set the delegate
    
            // Make our view the same size as the view we will be presenting in
            iAdInterstitialView.frame = self.view.bounds
        }
    
        func requestNewAd() {
            // This will fetch an ad for us
            ViewController.prepareInterstitialAds()
            println("Requesting new ad")
        }
    
        @IBAction func presentAdButton(sender: AnyObject) {
            if (adLoaded) {
                // We have an ad that is loaded so lets present it
                self.view.addSubview(iAdInterstitialView)
                iAdInterstitial.presentInView(iAdInterstitialView)
            }
            else {
                // No ad has been loaded
                println("Ad not loaded")
            }
        }
    
        func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
            // Kinda works as expected
            // Sometimes is called prematurely
            // Sometimes takes minutes after ad is dismissed to be called
            println("interstitialAdDidUnload")
    
            // Get new ad
            adLoaded = false
            iAdInterstitialView.removeFromSuperview()
            requestNewAd()
        }
    
        func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
            // Failed to load ad so lets try again
            println("didFailWithError: \(error)")
            requestNewAd()
        }
    
        func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
            // There is an ad and it has begun to download
            println("interstitialAdWillLoad")
        }
    
        func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
            // We got an ad
            println("interstitialAdDidLoad")
            adLoaded = true
        }
    
        func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
            println("interstitialAdActionShouldBegin")
            return true;
        }
    
        func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
            // Done with this ad. Lets get a new one
            println("interstitialAdActionDidFinish")
            iAdInterstitialView.removeFromSuperview()
            adLoaded = false
            requestNewAd()
        }
    

提交回复
热议问题