2016-11-07 8 views
0

у меня есть эта игра, и я создал 3 funcions в моем gameviewcontroller и здесь ониAdMob интерстициальное объявление никогда не готово

func getInterstitialAd(){ 
    interstitial = GADInterstitial(adUnitID: "ca-app-pub-1782852253088296/5018877964") 
    let requestInterstitial = GADRequest() 
    interstitial.load(requestInterstitial) 
} 



func showAd() { 

    if (interstitial.isReady == true){ 
     interstitial.present(fromRootViewController: GameViewController()) 
    }else{ 
     print("ad wasn't ready") 
     interstitial = createAd() 
    } 



} 

func createAd() -> GADInterstitial{ 
    let interstital = GADInterstitial(adUnitID: "ca-app-pub-1782852253088296/5018877964") 
    interstitial.load(GADRequest()) 
    return interstital 
} 

и в один из моей сцены называется STARTMENU, я называю тех, функцией

var viewController: GameViewController! 

, а затем я называю функцией

 viewController.getInterstitialAd() 
     viewController.showAd() 

, но он всегда возвращает объявление не готово, и ложное для interstit ial.isReady, , но также всегда вызывается функция getInterstitial.

может кто-то помочь с этим, пожалуйста,

+0

@ DanielStorm я удостоверился, что нет, и я действительно не знаю, как это исправить, пожалуйста, помогите –

ответ

1

Создать новый быстрый файл AdMobDelegate: -

import UIKit 
import GoogleMobileAds 

class AdMobDelegate: NSObject, GADInterstitialDelegate { 

    var interstitialView: GADInterstitial! 

    func createAd() -> GADInterstitial { 
     interstitialView = GADInterstitial(adUnitID: "Your Key") 
     interstitialView.delegate = self 
     let request = GADRequest() 
     interstitialView.loadRequest(request) 
     return interstitialView 
    } 

    func showAd() { 
     if interstitialView != nil { 
      if (interstitialView.isReady == true){ 
       interstitialView.present(fromRootViewController:currentVc) 
      } else { 
       print("ad wasn't ready") 
       interstitialView = createAd() 
      } 
     } else { 
      print("ad wasn't ready") 
      interstitialView = createAd() 
     } 
    } 

    func interstitialDidReceiveAd(ad: GADInterstitial!) { 
     print("Ad Received") 
     if ad.isReady { 
      interstitialView.present(fromRootViewController: currentVc) 
     } 
    } 

    func interstitialDidDismissScreen(ad: GADInterstitial!) { 
     print("Did Dismiss Screen") 
    } 

    func interstitialWillDismissScreen(ad: GADInterstitial!) { 
     print("Will Dismiss Screen") 
    } 

    func interstitialWillPresentScreen(ad: GADInterstitial!) { 
     print("Will present screen") 
    } 

    func interstitialWillLeaveApplication(ad: GADInterstitial!) { 
     print("Will leave application") 
    } 

    func interstitialDidFailToPresentScreen(ad: GADInterstitial!) { 
     print("Failed to present screen") 
    } 

    func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) { 
     print("\(ad) did fail to receive ad with error \(error)") 
    } 
} 

Теперь вы можете использовать объект этого делегата класса в других файлах следующим образом: -

//Define admobdelegate as global variable 
var admobDelegate = AdMobDelegate() 

//Declare a global variable currentVc to hold reference to current view controller 
var currentVc: UIViewController! 

class abc1: UIViewController { 

    override func viewdidload() { 
     super.viewdidload() 
     currentVc = self 
     admobDelegate.showAd() 
    } 

    override func viewDidAppear() { 
     super.viewDidAppear() 
     currentVc = self 
    } 
} 

class abc2: UIViewController { 

    override func viewdidload() { 
     super.viewdidload() 
     currentVc = self 
     admobDelegate.showAd() 
    } 

    override func viewDidAppear() { 
     super.viewDidAppear() 
     currentVc = self 
    } 
} 

Код находится в Swift 2.2. Запишите эквивалентный код в swift 3 в случае ошибки синтаксиса.

+0

большое вам спасибо, наконец, на работу !!! Я целыми днями раскачивал свой мозг, пытаясь понять это. –

+0

и еще один вопрос, если я хочу позвонить из другой функции, такой как gamescene, что это лучший способ сделать это? –

+0

просто вызовите showAd() в этой функции. Позаботьтесь сами по себе – Abhishek729