问题描述
我正在使用 Xcode 6 + Swift.
I'm using Xcode 6 + Swift.
有人知道如何在 Swift 项目中集成 Google Ads、GoogleMobileAdsSdkiOS?如何配置,如何编程?
Does anybody know how to integrate Google Ads, GoogleMobileAdsSdkiOS on a Swift Project? how to config, and how to program?
我在项目上新建了一个文件Teste-Bridging-Header.h
,把#import GADBannerView.h
基于 这个参考
I created a new file Teste-Bridging-Header.h
on the project and put #import GADBannerView.h
based on this reference
在我的 ViewController.swift
上类似:
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"
}
}
我的方向正确吗?我可以举一些例子吗?
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)
}
}
推荐答案
我设法通过使用这个来编译和工作:
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 (?)
}
为了让它工作,我必须确保在Swift 编译器 - 代码生成"部分中包含_-Bridging-Header.h"(带有您的项目名称),"Objective-C Bridging Header"字段(按照原贴的建议).
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).
这篇关于xcode 6 快速广告 GoogleMobileAdsSdkiOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!