I made a sample app and I'm currently trying to test admobs with a login before it. Here's my code:
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.")
}
}
I'll post a video of what's happening, but basically the ad audio plays, it just doesn't keep the actual ad on screen nor append the updates to the label.
https://www.youtube.com/watch?v=BlUjPA_AwZQ&feature=youtu.be
Use This Code....that code is working....
@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")
}
}