【问题标题】:Making Admob App on iOS, Getting Thread 1 Error在 iOS 上制作 Admob 应用程序,出现线程 1 错误
【发布时间】:2020-03-19 08:59:57
【问题描述】:

我在使用简单的视图控制器集成和初始化 Admob 时遇到问题。我收到一条错误消息,指出“线程 1:致命错误:在隐式展开可选值时意外发现 nil”。我已经使用 Google 的教程来了解如何启动 AdMob 应用,下面是我的相关代码。

import UIKit

import GoogleMobileAds

class ViewController: UIViewController, GADBannerViewDelegate {
    var bannerView: GADBannerView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    bannerView.adUnitID = "ca-app-pub-xxxxx/xxxxx" // line with error
    bannerView.rootViewController = self
    bannerView.load(GADRequest())
    // In this case, we instantiate the banner with desired ad size.
    bannerView = GADBannerView(adSize: kGADAdSizeBanner)


    bannerView.delegate = self

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


/// Tells the delegate an ad request loaded an ad.
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
    print("adViewDidReceiveAd")
}

/// Tells the delegate an ad request failed.
func adView(_ bannerView: GADBannerView,
            didFailToReceiveAdWithError error: GADRequestError) {
    print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}

/// Tells the delegate that a full-screen view will be presented in response
/// to the user clicking on an ad.
func adViewWillPresentScreen(_ bannerView: GADBannerView) {
    print("adViewWillPresentScreen")
}

/// Tells the delegate that the full-screen view will be dismissed.
func adViewWillDismissScreen(_ bannerView: GADBannerView) {
    print("adViewWillDismissScreen")
}

/// Tells the delegate that the full-screen view has been dismissed.
func adViewDidDismissScreen(_ bannerView: GADBannerView) {
    print("adViewDidDismissScreen")
}

/// Tells the delegate that a user click will open another app (such as
/// the App Store), backgrounding the current app.
func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
    print("adViewWillLeaveApplication")
}
let adSize = GADAdSizeFromCGSize(CGSize(width: 300, height: 50))
}

非常感谢!

【问题讨论】:

    标签: swift admob


    【解决方案1】:

    您必须在访问其属性之前初始化bannerView,向上移动初始化行。

    bannerView = GADBannerView(adSize: kGADAdSizeBanner) //<---- move up to here.
    bannerView.adUnitID = "ca-app-pub-xxxxx/xxxxx"
    bannerView.rootViewController = self
    bannerView.load(GADRequest())
    

    bannerView = GADBannerView(adSize: kGADAdSizeBanner)

    【讨论】:

    • 非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2016-04-11
    • 2015-10-07
    • 2021-05-19
    • 2015-02-27
    相关资源
    最近更新 更多