我试图在swift 3.0文件中添加一个admob横幅。我的项目是objc和swift 3.0的组合,但我遇到了问题。
这是我的代码:
resultSet.next()
编辑
好吧,所以除了谷歌示例中的3行代码之外我已经将所有内容划掉了条纹并且我得到以下错误:致命错误:在解开可选值时意外地发现了nil。我破坏了谷歌的例子,他们没有这个问题,这很奇怪。我没有GADBannerViewDelegate,因为谷歌的例子也没有。
func initAdMobBanner() {
if UIDevice.current.userInterfaceIdiom == .phone {
// iPhone
//adBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: 320, height: 50))
//adBannerView.frame = CGRect(x: 0, y: view.frame.size.height, width: view.frame.size.width, height: 50)
} else {
// iPad
//adBannerView.adSize = GADAdSizeFromCGSize(CGSize(width: 468, height: 60))
//adBannerView.frame = CGRect(x: 0, y: view.frame.size.height, width: view.frame.size.width, height: 60)
}
adBannerView.adUnitID = kadmobID
adBannerView.rootViewController = self
adBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
adBannerView.delegate = self
view.addSubview(adBannerView)
let request = GADRequest()
adBannerView.load(request)
}
// Hide the banner
func hideBanner(_ banner: UIView) {
UIView.beginAnimations("hideBanner", context: nil)
banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
UIView.commitAnimations()
banner.isHidden = true
}
// Show the banner
func showBanner(_ banner: UIView) {
UIView.beginAnimations("showBanner", context: nil)
banner.frame = CGRect(x: view.frame.size.width/2 - banner.frame.size.width/2, y: view.frame.size.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
UIView.commitAnimations()
banner.isHidden = false
}
// AdMob banner available
func adViewDidReceiveAd(_ bannerView: GADBannerView!) {
showBanner(adBannerView)
}
// NO AdMob banner available
func adView(_ bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
hideBanner(adBannerView)
print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}