Представьте новый ViewController после закрытия рекламного объявления - Swift 4 - PullRequest
0 голосов
/ 26 января 2019

Я пытаюсь заставить эту кнопку перейти к новому ViewController, как только пользователь закроет промежуточную рекламу.Код segue работает без рекламного кода, а рекламный код работает без кода segue.Что бы ни поставили первым, это произойдет первым, но я не могу понять, как заставить их работать вместе.Вы заметите, что я попытался переместить self.present... в разные места безуспешно (см. Отмеченные строки).

@IBAction func adButton(_ sender: UIButton) {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
    //self.present(myVC, animated: true, completion: nil)

    if (interstitial.isReady) {
        interstitial.present(fromRootViewController: self)
        interstitial = createAd()
        //self.present(myVC, animated: true, completion: nil)
    }
    self.present(myVC, animated: true, completion: nil)
}

1 Ответ

0 голосов
/ 26 января 2019

Объект interstitial имеет протокол делегата (GADInterstitialDelegate), которому вы можете соответствовать, чтобы получать уведомления об отклонении объявления.Существует несколько методов делегирования адреса состояния, которые вы можете просмотреть в документации.

class ViewController:UIViewController, GADInterstitialDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        interstitial.delegate = self
        let request = GADRequest()
        interstitial.load(request)
    }

    @IBAction func adButton(_ sender: UIButton) {

        if (interstitial.isReady) {
            interstitial.present(fromRootViewController: self)
        } else {
           self.present(myVC, animated: true, completion: nil)
       }
    }

    //Tells the delegate the interstitial is to be animated off the screen.
    func interstitialWillDismissScreen(_ ad: GADInterstitial) {
        print("interstitialWillDismissScreen")
    }

    //Tells the delegate the interstitial had been animated off the screen.
    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        print("interstitialDidDismissScreen")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
        self.present(myVC, animated: true, completion: nil)
    }
}

Документы Google Ads: Промежуточные объявления

...