美文网首页
2019-在iOS里添加admob横幅广告示例

2019-在iOS里添加admob横幅广告示例

作者: yytester | 来源:发表于2019-08-25 21:34 被阅读0次
  1. 下载sdk , 解压

  2. 导入项目文件夹: image.png
    image.png
  3. 在info.plist里加入应用id(不是广告单元id): GADApplicationIdentifier

    image.png
  4. 设置Build Settings选项


    image.png
  5. 设置appDelegate:

import GoogleMobileAds


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        GADMobileAds.sharedInstance().start(completionHandler: nil)

        
        return true
    }
  1. 设置ViewController:
import UIKit
import GoogleMobileAds


class ViewController: UIViewController,GADBannerViewDelegate {
    var bannerView: GADBannerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        bannerView = GADBannerView(adSize: kGADAdSizeBanner)
        bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        bannerView.rootViewController = self
        bannerView.load(GADRequest())

        bannerView.delegate = self

        addBannerViewToView(bannerView)
        
        
    }
    
    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)
            ])
    }


}



参考: 开始使用 | iOS | Google Developers

相关文章

网友评论

      本文标题:2019-在iOS里添加admob横幅广告示例

      本文链接:https://www.haomeiwen.com/subject/tpdkectx.html