如何通过点击按钮每次都能展示AdMob广告?是否可以设置广告的点击次数或广告时间?
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
@IBOutlet var BannerView: GADBannerView!
var interstitial: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
interstitial = GADInterstitial(adUnitID: "ca-app-pub-1469592343938512/3581855984")
let request2 = GADRequest()
interstitial.loadRequest(request2)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
BannerView.delegate = self
BannerView.adUnitID = "ca-app-pub-1469592343938512/2613153588"
BannerView.rootViewController = self
BannerView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func createAD() -> GADInterstitial{
let interstitial = GADInterstitial(adUnitID: "ca-app-pub-1469592343938512/3581855984")
interstitial.loadRequest(GADRequest())
return interstitial
}
@IBAction func ShowAD(sender: AnyObject) {
if (interstitial.isReady){
interstitial.presentFromRootViewController(self)
interstitial = createAD()
}
}
}
答案 0 :(得分:0)
您可以设置概率机制,从1到100提取随机数。 如果数字低于某个阈值,则显示广告否则不显示。 例如,阈值是30,您显示广告的概率为30%。 像这样:
func shouldShowAd(chance: Int ) -> Bool {
//extract number from 0 to 100
//arc4random_uniform(UInt32(max - min + 1))
let extracted = arc4random_uniform(101)
return chance >= Int(extracted)
}
// set the threshold from 0 (never show ads) to 100 (always show ads)
// for example a chance of 40 is 40% of probability of showing ads
@IBAction func ShowAD(sender: AnyObject) {
if shouldShowAd(chance: 40) {
if (interstitial.isReady){
interstitial.presentFromRootViewController(self)
interstitial = createAD()
}
}
}