LoginSignup
0
3

More than 3 years have passed since last update.

【Swift】【Xcode】adMobの広告を入れるためのコード

Last updated at Posted at 2020-01-26

SwiftでのadMobの広告の入れ方(方法)

すべてを端折ってadMobの広告を入れる際に必要なコードだけにした備忘録

💡注意💡
コード内にも記載しているがID系はGitにpushする際に記載したままpushしちゃわないように!
載せちゃうと、、、アカウントがキンキンになる

admob.swift
import GoogleMobileAds

class mapMapViewController: GADBannerViewDelegate {
    // GADBannerViewDelegate をプロトコルとして入れる

    // AdMob ID を入れる  
    let AdMobID = ""
    let TEST_DEVICE_ID = "xxxxxxxxxxxxxxxxxxxxxxx"
    //ID系はgitにpushする際載せないように注意!

    let AdMobTest:Bool = true      
    let SimulatorTest:Bool = true    
    //testのときはtrueにしておく
    //実機or審査に提出の際はfalseにする

override func viewDidLoad() {
        super.viewDidLoad()

        showAdBanner()
       //広告を表示する関数
    }

 //広告を表示する
    func showAdBanner(){

        var admobView = GADBannerView()
        admobView = GADBannerView(adSize:kGADAdSizeBanner)
        admobView.frame.origin = CGPoint(x:0, y:self.view.frame.size.height - admobView.frame.height - 45)

        admobView.frame.size = CGSize(width:self.view.frame.width, height:admobView.frame.height)
        admobView.adUnitID = AdMobID
        admobView.delegate = self
        admobView.rootViewController = self

        let admobRequest = GADRequest()

        if(AdMobTest){
            // simulator テスト
            if SimulatorTest {
                admobRequest.testDevices = [kGADSimulatorID]
                print("simulator")
            }
                // 実機テスト
            else {
                admobRequest.testDevices = [TEST_DEVICE_ID]
                print("device")
            }
        }
            // 本番
            admobView.load(admobRequest)

        self.view.addSubview(admobView)
    }

      override func viewWillAppear(_ animated: Bool) {
        read()
    }

}

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3