1.下載谷歌ADs的SDK或者pod 'Google-Mobile-Ads-SDK'
2.導入頭文件 import GoogleMobileAds
3.創建//adUnitID 需要去谷歌ADs官網申請
var bannerView: GADBannerView! //定義全局變量
//谷歌ADs設置相關參數
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
addBannerViewToView(bannerView)
bannerView.adUnitID = "ca-app-pub-9033627101784845/4539885860"//ca-app-pub-9033627101784845/4539885860
bannerView.rootViewController = self
bannerView.load(GADRequest())
//設置廣告相關位置
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}