Swift4 Day79:App廣告教學

Swift4 / iOS11 / Get Started | AdMob for iOS | Google Developers/橫幅廣告跟插頁式廣告教學/2017.02.11

Alice
Daily Swift
5 min readFeb 11, 2018

--

完成的樣子:這次是橫幅廣告跟插頁式廣告教學

Get Started | AdMob for iOS | Google Developers

AdMob是最常見的Google廣告安裝

Install AdMob SDK and configure the App Delegate

跟著文件走cocoapod安裝

1.cd到資料夾 2.$pod init 3.open -a Xcode Podfile放入以下SDK

4.pod install 5.打開xcworkspace的檔案

跟著文件加入以下兩個只是

import GoogleMobileAds

GADMobileAds.configure(withApplicationID: “YOUR_ADMOB_APP_ID”)

Create app ID and a sample banner to use in the app

admob建立帳號

建立完之後你就會拿到YOUR_ADMOB_APP_ID,複製起來。

可以看到常見的廣告模式,然後我們選擇橫幅廣告。

Display banner ad and request test ads

在UI下方加上view,然後將class設定成GADBannerView

要去AdMob登入個人資料才會出現Ad,不然一直bulid也沒有QQ

在console會看到<Google> To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ]

但用swift的話不需要@,寫在let request = GADRequest()下

request.testDevices = [ kGADSimulatorID ]

就會出現右側的測試Test Ad

Learn how to use the banner view delegates

Implementing banner events提到

在 bannerView.delegate = self

可以用extension加上以下的func讓用戶在與Ad互動時有更多提示反饋

像這樣~

建立全幅蓋板廣告interstitial

要先把讀取的內容放在viewdidload裡面去跑,如果把所有東西放在button裡面的話是會動不了的,但是這個方法只能讀取廣告一次。

(等等會debug)

蓋版廣告interstitial一定要用:request.testDevices = [ kGADSimulatorID ]

這個步驟到這裡的程式碼

無限次全幅蓋板廣告interstitial

將事前建立的code用createInterstitial包起來

關掉廣告的同時就讓 interstitial = createInterstitial()

讀入時也直接createInterstitial()

完整github

Register at admob.com and login — https://apps.admob.com/v2/home

Developer website — https://developers.google.com/admob/ios/quick-start

Interstitial — https://developers.google.com/admob/ios/interstitial

--

--