Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@Marie23

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

More than 1 year has passed since last update.

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()
    }

}

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?