xcode 6 swift ads GoogleMobileAdsSdkiOS

2019-04-01 17:59发布

问题:

I'm using Xcode 6 + Swift.

Does anybody know how to integrate Google Ads, GoogleMobileAdsSdkiOS on a Swift Project? how to config, and how to program?

I created a new file Teste-Bridging-Header.h on the project and put #import GADBannerView.h based on this reference

on my ViewController.swift some thing like:

class ViewController: UIViewController {   
     override func viewDidAppear(animated: Bool)  {
        super.viewWillAppear(animated)    

        var adB = GADBannerView()
        adB.delegate = self
        adB.rootViewController = self
        adB.adUnitID =  MY_ADS_ID //"ca-app-pub-XXXXXXXX/XXXXXXX"       
     }
}

Am I going in the right direction? Can I get some examples?

now its working...

on .h created  Objective-C bridging header I put: 
#import "GADBannerView.h"
#import "GADBannerViewDelegate.h"
#import "GADRequest.h"

my swift file:

    class ViewController: UIViewController, GADBannerViewDelegate {

         override func viewDidAppear(animated: Bool)  {
            super.viewWillAppear(animated)

            var adB = GADBannerView( frame:CGRectMake(0, 0, 320, 50) )
            adB.delegate = self
            adB.rootViewController = self
            adB.adUnitID = "ca-app-pub-XXXXXXX/XXXXXXXX"

            var reqAdB = GADRequest()
            //reqAdB.testDevices = [ GAD_SIMULATOR_ID ] //not working, dont know why
            //reqAdB.testDevices = ["Simulator"] //not working, dont know why

            adB.loadRequest(reqAdB)        
            self.view.addSubview(adB)

         }

    }

回答1:

I managed to get everything compiling and working by using this:

override func viewDidAppear(animated: Bool)  {
    super.viewDidAppear(animated)

    var origin = CGPointMake(0.0,
        self.view.frame.size.height -
            CGSizeFromGADAdSize(kGADAdSizeBanner).height); // place at bottom of view

    var size = GADAdSizeFullWidthPortraitWithHeight(50) // set size to 50
    var adB = GADBannerView(adSize: size, origin: origin) // create the banner
    adB.adUnitID = MY_BANNER_UNIT_ID  //"ca-app-pub-XXXXXXXX/XXXXXXX"
    adB.delegate = self // ??
    adB.rootViewController = self // ??
    self.view.addSubview(adB) // ??
    var request = GADRequest() // create request
    request.testDevices = [ GAD_SIMULATOR_ID ]; // set it to "test" request
    adB.loadRequest(request) // actually load it (?)
}

To get it to work, I had to make sure to include "_-Bridging-Header.h" (with your project name) in the "Swift Compiler - Code Generation" section, "Objective-C Bridging Header" field (as original posted suggested).



回答2:

You don't need to do all the Objective-C bridging stuff. I also had this problem and had to search the framework files for clues. I found a comment that included the updated definition name: kDFPSimulatorID

So just change it to this:

request.testDevices = [kDFPSimulatorID]

Should work.