ios 谷歌广告

在swift中使用
1,pod 'Firebase/AdMob',
2, 导入头文件 #import"Firebase.h",在桥接文件中
3,HomeViewController.swift中

class HomeViewController : UIViewController{
    var interstitial : GADInterstitial!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.createAndLoadInterstitial()
    }
    func clickADButton(){
        if self.interstitial == nil {
            self.createAndLoadInterstitial()
        }
        if self.interstitial.isReady {
            dispatch_async(dispatch_get_main_queue(), {
                self.interstitial.presentFromRootViewController(self)
            })
        }
    }
    //
func createAndLoadInterstitial() {
        self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-8540695118272502/8273251709")
        self.interstitial.delegate = self
        let request = GADRequest()
        //测试设备号,如果不知道,编译后控制台可以看见真机的设备号
        request.testDevices = [kGADSimulatorID, "设备id"]
        self.interstitial.loadRequest(request)
    }
}

//MARK: - GADInterstitialDelegate
extension HomeConnectController : GADInterstitialDelegate{
    func interstitialDidDismissScreen(ad: GADInterstitial) {
        self.createAndLoadInterstitial()
    }
    func interstitialDidFailToPresentScreen(ad: GADInterstitial) {
        self.createAndLoadInterstitial()
    }
}

你可能感兴趣的:(ios 谷歌广告)