Swift - 谷歌ADs(BannerView) 原

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章