AdMob AD视图控制器在打开后立即关闭

时间:2018-03-13 03:21:06

标签: ios swift admob viewcontroller

长期潜伏并快速发展。我做了一个示例应用程序,我正在尝试使用登录之前测试admobs。这是我的代码:

AppDelegate.swift

import UIKit
import Firebase
import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        GADMobileAds.configure(withApplicationID: "ca-app-pub-3940256099942544~1458002511")
        return true
    }



}

ViewController.swift

import UIKit
import FirebaseAuth

class ViewController: UIViewController {

    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        if(Auth.auth().currentUser != nil)
        {
            self.presentLoggedInScreen()
        }
    }

    @IBAction func createAccountTapped(_ sender: Any)
    {
            if let email = emailTextField.text, let password = passwordTextField.text{
                Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
                    if let firebaseError = error {
                        let alert = UIAlertController(title: "Error", message: firebaseError.localizedDescription, preferredStyle: .alert)
                        alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .`default`, handler: { _ in
                        }))
                        self.present(alert, animated: true, completion: nil)
                        return
                    }
                    self.presentLoggedInScreen()
                })
            }

    }

    @IBAction func loginTapped(_ sender: Any)
    {
        if let email = emailTextField.text, let password = passwordTextField.text{
            Auth.auth().signIn(withEmail: email, password: password, completion: { user, error in
                if let firebaseError = error {
                    let alert = UIAlertController(title: "Error", message: firebaseError.localizedDescription, preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .`default`, handler: { _ in
                    }))
                    self.present(alert, animated: true, completion: nil)
                    return
                }
                self.presentLoggedInScreen()
            })
        }

    }

    func presentLoggedInScreen()
    {
        let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let loggedInVC:LoggedInVC = storyboard.instantiateViewController(withIdentifier: "LoggedInVC") as! LoggedInVC
        self.present(loggedInVC, animated: true, completion: nil)
    }


}

LoggedInVC.swift

import UIKit
import FirebaseAuth
import GoogleMobileAds

class LoggedInVC: UIViewController, GADRewardBasedVideoAdDelegate {

    @IBOutlet weak var showAdButton: UIButton!
    @IBOutlet weak var logLabel: UILabel!
    var rewardBasedAd: GADRewardBasedVideoAd!

    override func viewDidLoad() {
        super.viewDidLoad()
        logLabel.text = ""
        showAdButton.isEnabled = false

        rewardBasedAd = GADRewardBasedVideoAd.sharedInstance()
        rewardBasedAd.delegate = self
        rewardBasedAd.load(GADRequest(), withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
    }

    @IBAction func logoutTapped(_ sender: Any) {
        do{
           try Auth.auth().signOut()
           dismiss(animated: true, completion: nil)
        } catch{
            print("Problem logging out!");
        }
    }

    @IBAction func buttonPressed(_ sender: Any) {
        showAdButton.isEnabled = false
        if rewardBasedAd.isReady {
            rewardBasedAd.present(fromRootViewController: self)
        }
    }

    func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        logLabel.text?.append("An ad opened. \n")
    }

    func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        logLabel.text?.append("An ad closed. \n")
    }

    func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        logLabel.text?.append("An ad has loaded. \n")
        showAdButton.isEnabled = true
    }

    func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        logLabel.text?.append("An ad started playing. \n")
    }

    func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        logLabel.text?.append("An ad caused focus to leave. \n")
    }

    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didFailToLoadWithError error: Error) {
        logLabel.text?.append("An ad has failed to load. \n")
        print(error)
    }

    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
        print("Ad finished.")
    }
}

现在有人说我没有研究解决方案,相信我,我已经尝试过了。这真的让我退缩,真的不确定为什么会这样。这可能是对某人的简单修复,但作为一个初学者,答案对我来说并不明显:(。

我会发布一个正在发生的事情的视频,但基本上是播放广告音频,它不会将实际广告保留在屏幕上,也不会将更新附加到标签上。对此有任何帮助将非常感激,这是一个了不起的社区。

https://www.youtube.com/watch?v=BlUjPA_AwZQ&feature=youtu.be

1 个答案:

答案 0 :(得分:1)

  

使用此代码....该代码正在运行....

@IBAction func buttonPressed(_ sender: Any) {
    //showAdButton.isEnabled = false
    if rewardBasedAd.isReady {
        self.dismiss(animated: false) { () -> Void in
            rewardBasedAd.present(fromRootViewController: self)
        }
    }
    else {
        print("Ad wasn't ready")
    }
}