在 xcode 6 中使用 swift 的 AdMob 横幅
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26704362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AdMob banner using swift in xcode 6
提问by user1759949
I've successfully used admobin previous apps using objective cbut I seem to be having problems getting this to work in swift.
我已经成功地在以前的应用程序中使用了目标 c使用了admob,但我似乎在让它在swift 中工作时遇到了问题。
Using the following code in viewDidAppear
will successfully show a test ad.
在中使用以下代码viewDidAppear
将成功显示测试广告。
var adB = GADBannerView(frame:CGRectMake(0, 20, 320, 50)) // create the banner
adB.adUnitID = "ca-app-pub-xxxxxxxx/xxxxxxx"
adB.delegate = self
adB.rootViewController = self
var request = GADRequest()
request.testDevices = [GAD_SIMULATOR_ID];
adB.loadRequest(request)
self.view.addSubview(adB)
Some help for this came from the following question: xcode 6 swift ads GoogleMobileAdsSdkiOS
对此的一些帮助来自以下问题: xcode 6 swift ads GoogleMobileAdsSdkiOS
However my issue is when I want to remove the test ads in preparation for the app store. In my previous apps the only difference I can see is that I removed the lines for "request" and have the following line instead:
但是我的问题是当我想删除测试广告以准备应用商店时。在我以前的应用程序中,我能看到的唯一区别是我删除了“请求”行并改为使用以下行:
[bannerView_ loadRequest:[GADRequest request]];
Trying this in swift:
快速尝试这个:
adB.loadRequest(GADRequest().request)
brings up the following error:
出现以下错误:
"GADRequest does not have a member named 'request'"
Leaving 'request' out does not bring up any ads. Has anyone had any success with admob using swift to bring up live ads?
将“请求”排除在外不会显示任何广告。有没有人在使用 swift 显示实时广告的 admob 方面取得过任何成功?
回答by Michael
this is how I successfully load live ads with swift:
这就是我使用 swift 成功加载实时广告的方式:
bannerDisplayed = false
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView?.adUnitID = "ca-app-pub-blub"
bannerView?.delegate = self
bannerView?.rootViewController = self
self.view.addSubview(bannerView!)
var request:GADRequest = GADRequest()
if testRun {
var devices: [String] = ["abc", "xyze"]
request.testDevices = devices
}
bannerView?.loadRequest(request)