在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:04:36  来源:igfitidea点击:

AdMob banner using swift in xcode 6

iosxcodeswiftadmob

提问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 viewDidAppearwill 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)