【发布时间】:2016-03-10 07:44:34
【问题描述】:
尝试在我的 Swift、SpriteKit、iOS9 游戏中放置 admob 插页式广告时,我遇到了致命的崩溃。
我相信我已经按照此处的简化说明通过 CocoaPods 正确设置了 Admob SDK 和依赖项: 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 说明中的测试代码),但没有骰子 - 同样的错误。
任何建议将不胜感激。
干杯, 凯文
【问题讨论】:
标签: swift sprite-kit admob ios9