2016-09-24 85 views
1

如何通过单击按钮不是每次显示AdMob广告?是否有可能在广告点击次数或广告显示的时间之后设置?不是每次都显示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

您可以设置的概率机制,提取从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() 
     } 
    } 
} 
+0

你能给我一个适合我的代码的例子吗? –

+0

我会发表一个编辑原始答案的例子 – iGenio

+0

能否请你给我一个例子与上面的代码我总是会得到错误... –