2016-09-24 107 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

能否請你給我一個例子與上面的代碼我總是會得到錯誤... –