Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active September 5, 2016 04:19
Show Gist options
  • Save KentarouKanno/ec5b09fc63bc08f49886dd849e9ad0a0 to your computer and use it in GitHub Desktop.
Save KentarouKanno/ec5b09fc63bc08f49886dd849e9ad0a0 to your computer and use it in GitHub Desktop.
Admob

Admob

★ Library

AdSupport
AudioToolbox
AVFoundation
CoreGraphics
CoreMedia
CoreTelephony
EventKit
EventKitUI
MessageUI
StoreKit
SystemConfiguration

★ 設定

import UIKit

// import iAd
import GoogleMobileAds

class ViewController: UIViewController, GADInterstitialDelegate {
    
    var _interstitial: GADInterstitial?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Initialize
        _interstitial = createAndLoadInterstitial()
        
        //ボタン作成(ボタン押下時に広告を表示)
        let adButton = UIButton(type: .Custom)
        adButton.backgroundColor = UIColor.redColor()
        adButton.frame = CGRectMake(100, 200, 100, 60)
        adButton.setTitle("ad", forState: .Normal)
        adButton.addTarget(self, action: #selector(ViewController.presentInterstitial(_:)), forControlEvents: .TouchUpInside)
        self.view.addSubview(adButton)
        
    }
    
    func interstitialDidReceiveAd(ad: GADInterstitial!) {
        print("interstitialDidReceiveAd")
    }
    
    func interstitialWillDismissScreen(ad: GADInterstitial!) {
        // 全画面広告が非表示になる前に呼ばれる
        print("interstitialWillDismissScreen")
    }
    
    func interstitialDidDismissScreen(ad: GADInterstitial!) {
        // 全画面広告が非表示になった後に呼ばれる
        print("interstitialDidDismissScreen")
        
        
        // 毎回出すのであれば再度設定する
        _interstitial = createAndLoadInterstitial()
    }
    
    
    func createAndLoadInterstitial() -> GADInterstitial {
        
        print("createAndLoadInterstitial")
        let interstitial = GADInterstitial(adUnitID: "ca-app-pub-XXXXXXXXXXXXXXXXXX")
        interstitial.delegate = self
        let gadRequest:GADRequest = GADRequest()
        gadRequest.testDevices = [kGADSimulatorID]
        interstitial.loadRequest(gadRequest)
        
        return interstitial
    }
    
    func presentInterstitial(sender:UIButton!) {
        if _interstitial!.isReady {
            // 準備が出来ている時にのみ実行される(広告表示)
            _interstitial?.presentFromRootViewController(self)
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment