ios-swift版添加admob-插页广告示例

1 以一个列表A为例, 点击列表A的某个项,进入下一个子页面B时,弹出插页广告.

  1. 下载sdk,解压.
  2. 添加解压后的framework文件到项目里.


    Paste_Image.png
  3. 在列表A和子页面B的viewcontroller文件里定义变量var interstitial: GADInterstitial!
  4. 在列表A的viewcontroller文件定义函数createAndLoadInterstitial.
    public func createAndLoadInterstitial() {
         //需要改成自己的广告id
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let request = GADRequest()
 
        request.testDevices = [ kGADSimulatorID, "2077ef9a63d2b398840261c8221a0c9a" ]
        interstitial.load(request)
    }
  1. 在列表A的controller文件viewDidLoad()里调用方法createAndLoadInterstitial:
    createAndLoadInterstitial()
  2. 在列表A.viewcontroller的链接方法prepare里, 将当前页面获取的interstitial值,传递给子页面.
            let detailVC = segue.destination as! BViewController
            detailVC.interstitial = interstitial

同时,因为之前生成的interstitial是一次性的.所以需要再次调用createAndLoadInterstitial()

  1. 在B.viewcontroller的viewdidload方法里进行调用:
        if interstitial.isReady {
            interstitial.present(fromRootViewController: self)
        } else {
            print("Ad wasn't ready")
        }
与本文不相关内容删除后的AViewController代码:
import UIKit
import GoogleMobileAds

class AViewController: UITableViewController {

    var interstitial: GADInterstitial!

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

    }
    
    public func createAndLoadInterstitial() {
        interstitial = GADInterstitial(adUnitID: "xxxxxxxx")
        let request = GADRequest()
 
        request.testDevices = [ kGADSimulatorID, "xxxxx" ]
        interstitial.load(request)
    }
 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if (segue.identifier == "xxx") {
  
            let detailVC = segue.destination as! BViewController
            detailVC.interstitial = interstitial
        }    
        createAndLoadInterstitial()
    }

}
与本文不相关内容删除后的BViewController代码:
import UIKit
import GoogleMobileAds

class BViewController: UITableViewController  {
    var interstitial: GADInterstitial!
    override func viewDidLoad() {
        super.viewDidLoad()
        if interstitial.isReady {
            interstitial.present(fromRootViewController: self)
        } else {
            print("Ad wasn't ready")
        }    
    }

//B中定义的createAndLoadInterstitial并没有被调用,但是如果不定义则会产生各种奇葩报错,不知道为什么...
    public func createAndLoadInterstitial() {
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let request = GADRequest()
        // Request test ads on devices you specify. Your test device ID is printed to the console when
        // an ad request is made.
        request.testDevices = [ kGADSimulatorID, "2077ef9a63d2b398840261c8221a0c9a" ]
        interstitial.load(request)
    }

}

2 以一个button A为例, 点击button A的某个项,弹出插页广告.

这个比较简单,直接贴代码:

import UIKit
import GoogleMobileAds

class ViewController: UIViewController , GADInterstitialDelegate{
    
    var interstitial: GADInterstitial!

    @IBAction func buttonaction(_ sender: Any) {
        

        if interstitial.isReady {
            interstitial.present(fromRootViewController: self)
        } else {
            print("Ad wasn't ready")
        }

        createAndLoadInterstitial()  //点击按钮后,重新生成新的interstitial ,否则无法再次出现广告

    }
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        createAndLoadInterstitial()
        
    }
    
    fileprivate func createAndLoadInterstitial() {
        NSLog("进入广告函数")
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let request = GADRequest()
        // Request test ads on devices you specify. Your test device ID is printed to the console when
        // an ad request is made.
        request.testDevices = [ kGADSimulatorID, "2077ef9a63d2b398840261c8221a0c9a" ]
        interstitial.load(request)
    }
    

    @IBOutlet weak var buttonaction: UIButton!
    


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

你可能感兴趣的:(ios-swift版添加admob-插页广告示例)