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