nolang

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

  1. 下载sdk , 解压


  2. 导入项目文件夹: image.png

     image.png


  3. 在info.plist里加入应用id(不是广告单元id): GADApplicationIdentifier

    image.png


  4. 设置Build Settings选项

    image.png


  5. 设置appDelegate:
import GoogleMobileAds<br>
<br>
<br>
&nbsp; &nbsp; func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -&gt; Bool {<br>
&nbsp; &nbsp; &nbsp; &nbsp; // Override point for customization after application launch.<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>
&nbsp; &nbsp; &nbsp; &nbsp; GADMobileAds.sharedInstance().start(completionHandler: nil)<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>
&nbsp; &nbsp; &nbsp; &nbsp; return true<br>
&nbsp; &nbsp; }<br>
```<br>
6. 设置ViewController:<br>
```<br>
import UIKit<br>
import GoogleMobileAds<br>
<br>
<br>
class ViewController: UIViewController,GADBannerViewDelegate {<br>
&nbsp; &nbsp; var bannerView: GADBannerView!<br>
&nbsp; &nbsp;&nbsp;<br>
&nbsp; &nbsp; override func viewDidLoad() {<br>
&nbsp; &nbsp; &nbsp; &nbsp; super.viewDidLoad()<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp; bannerView = GADBannerView(adSize: kGADAdSizeBanner)<br>
&nbsp; &nbsp; &nbsp; &nbsp; bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"<br>
&nbsp; &nbsp; &nbsp; &nbsp; bannerView.rootViewController = self<br>
&nbsp; &nbsp; &nbsp; &nbsp; bannerView.load(GADRequest())<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp; bannerView.delegate = self<br>
<br>
&nbsp; &nbsp; &nbsp; &nbsp; addBannerViewToView(bannerView)<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>
&nbsp; &nbsp; }<br>
&nbsp; &nbsp;&nbsp;<br>
&nbsp; &nbsp; func addBannerViewToView(_ bannerView: GADBannerView) {<br>
&nbsp; &nbsp; &nbsp; &nbsp; bannerView.translatesAutoresizingMaskIntoConstraints = false<br>
&nbsp; &nbsp; &nbsp; &nbsp; view.addSubview(bannerView)<br>
&nbsp; &nbsp; &nbsp; &nbsp; view.addConstraints(<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [NSLayoutConstraint(item: bannerView,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attribute: .bottom,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; relatedBy: .equal,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toItem: bottomLayoutGuide,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attribute: .top,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; multiplier: 1,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; constant: 0),<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NSLayoutConstraint(item: bannerView,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attribute: .centerX,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; relatedBy: .equal,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toItem: view,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attribute: .centerX,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; multiplier: 1,<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; constant: 0)<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ])<br>
&nbsp; &nbsp; }<br>
<br>
<br>
}<br>
<br>
<br>
```<br>
<br>
<br>
<br>
<br>
---<br>
<br>
<br>
<br>
参考: [开始使用 &nbsp;|&nbsp;&nbsp;iOS &nbsp;|&nbsp;&nbsp;Google Developers](https://developers.google.com/admob/ios/quick-start#update_your_infoplist)

posted on 2019-08-26 13:34  全栈测试之路  阅读(721)  评论(0编辑  收藏  举报

导航