mercredi 15 juin 2016

Swift: About Protocols and Delegation pattern

i want to ask that how the protocols and delegation patterns functions in Swift.

I have an application that let me try the google ad sdk on iOS platform. But i'm missing something and confused about how the methods works.

I have some codes like these;

import UIKit
import GoogleMobileAds

class ViewController: UIViewController, GADInterstitialDelegate {

@IBOutlet weak var bannerView: GADBannerView!
let request = GADRequest()
var interstitial: GADInterstitial!

@IBOutlet weak var mylbl: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    bannerView.adUnitID = "xxx"
    bannerView.rootViewController = self
    bannerView.loadRequest(self.request)
    interstitial = createAndLoadInterstitial()
}

func createAndLoadInterstitial() -> GADInterstitial {
    let interstitial = GADInterstitial(adUnitID: "xxx")
    interstitial.delegate = self
    interstitial.loadRequest(self.request)
    return interstitial
}

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    interstitial = createAndLoadInterstitial()
    mylbl.text = "No ad"

}

func interstitialDidReceiveAd(ad: GADInterstitial!) {
    mylbl.text = "received ad"
}

@IBAction func touched(sender: AnyObject) {

    if interstitial.isReady
    {
        interstitial.presentFromRootViewController(self)
    }
    else
    {
        mylbl.text = "Not Ready!"
    }
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

For the code above, i'm aware of that the protocols blueprints of methods and properties to adopt a class or struct or enum. The methods or properties defined in the protocol should be implemented on the class that adopted by the related delegate.

I want to ask that and cofused point: OK the method which is named "interstitialDidDismissScreen" inherited from the delegate "GADInsterstitialDelegate" but how the method handled by pressing the close button of the interstitial ad. Where the engineers of Google implemented and how they succeed this behavior. Thanks for your help.

Good hacks,

Aucun commentaire:

Enregistrer un commentaire