Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import GoogleMobileAds
class TableViewController: UIViewController, UITableViewDelegate, GADBannerViewDelegate {
var bannerView: GADBannerView?
override func viewDidLoad() {
super.viewDidLoad()
//adSize有多種選擇,可以去官方文件裡面參考,這邊用的是iphone直立使用的size
bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
//下面這邊unitID,請填如你自己的,或是官方給的測試ID
bannerView?.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView?.delegate = self
bannerView?.rootViewController = self
bannerView?.load(GADRequest())
}
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
//如果有印出這一行,表示你成功從AdMod那邊下載到廣告
print("Banner loaded successfully")
}
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
//這邊就是我提到會印出"no ad to show"的地方,可以用我上面提到的兩個部分去修改看看
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//這邊會讓你的banner固定在Footer,而且使用在滑動的時候他都一直會在
//你把Footer改成header,也是一樣會成功。
return bannerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return bannerView!.frame.height
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment