当我试图在我的Swift,SpriteKit,iOS9游戏中放置一个admob间隙广告时,我遇到了致命的崩溃。
我相信我已经通过CocoaPods正确地设置了Admob和依赖项,按照这里的简化说明:https://developers.google.com/admob/ios/quick-start
我的个人档案:
platform :ios, '7.0'
use_frameworks!
source 'https://github.com/CocoaPods/Specs.git'
pod 'Google-Mobile-Ads-SDK', '~> 7.0'
target 'My Project's Name' do
end然后,我遵循以下指南:https://developers.google.com/admob/ios/interstitial
这是一个游戏,所以我把代码放在GameViewController.swift (而不是ViewController.swift)中。相关双边投资条约:
import GoogleMobileAds
class GameViewController: UIViewController, GADInterstitialDelegate {
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
self.interstitial = createAndLoadInterstitial()
// unrelated code
}
}
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
interstitial.loadRequest(GADRequest())
return interstitial
}
func interstitialDidDismissScreen(ad: GADInterstitial!) {
self.interstitial = createAndLoadInterstitial()
}
func gameOver() {
if self.interstitial.isReady {
self.interstitial.presentFromRootViewController(self)
}
}我在控制台中得到了正确的输出:Google Mobile Ads SDK version: afma-sdk-i-v7.7.0
但是,我在gameOver()函数上得到了这个错误:
fatal error: unexpectedly found nil while unwrapping an Optional value它似乎在if self.interstitial.isReady线上。
我确实在这里详细介绍了应用程序传输安全(ATS)津贴:https://developers.google.com/admob/ios/ios9
XML看起来像:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>我还试着用我的实际广告ID从上面的广告单元ID(测试代码Google在说明中)中更改广告单元ID,但是没有骰子-相同的错误。
如有任何建议,将不胜感激。
干杯,凯文
发布于 2016-03-10 12:11:24
您是否正在检查广告是否已正确预加载,因此不是零?
您还可以在...isReady行之前添加一个检查
guard self.interstitial != nil else { return }确保这不是零。
如果你还被卡住了,我在gitHub上有个广告助手帮你结帐,它还包括adMob
https://github.com/crashoverride777/Swift2-iAds-AdMob-CustomAds-Helper
https://stackoverflow.com/questions/35910433
复制相似问题