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