简体   繁体   中英

AdMob banner using swift in xcode 6

I've successfully used admob in previous apps using objective c but I seem to be having problems getting this to work in swift .

Using the following code in viewDidAppear will successfully show a test ad.

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

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?

this is how I successfully load live ads with 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM