4

i have this game and i created 3 funcions in my gameviewcontroller and here they are

    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
}

and in one of my scene called StartMenu , i call those function

   var viewController: GameViewController!

and then i call the functions

       viewController.getInterstitialAd()
        viewController.showAd()

but it always returns ad not ready , and false for interstitial.isReady, but also the getInterstitial function is always called .

can someone help with that please

Naeim Salib
  • 75
  • 1
  • 6

1 Answers1

4

Create a new swift file 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)")
    }
}

Now you can use the object of this delegate class in other files as follows :-

//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
    }
}

Code is in Swift 2.2. Write your equivalent code in swift 3 in case of syntax error.

Abhishek729
  • 327
  • 5
  • 14
  • thank you so much , finally got to work !!! i've been rocking my brain for days trying to figure it out . – Naeim Salib Nov 07 '16 at 14:08
  • and just one more question , if i wanted to call from another function like gamescene , what is the best way i could do it? – Naeim Salib Nov 07 '16 at 14:10
  • just call showAd() in that function. Should take care by itself – Abhishek729 Nov 07 '16 at 14:11
  • in my gamescene i created an object var viewController: GameViewController! and then called it like that viewController.showAd() but the game crashed – Naeim Salib Nov 07 '16 at 14:13
  • gamescene is a function within the same class or a different class? – Abhishek729 Nov 07 '16 at 14:14
  • no it's a different class – Naeim Salib Nov 07 '16 at 14:15
  • easier but poor solution is to copy paste this code in that class. A better way will be to create a seperate delegate handler class for admob ads. – Abhishek729 Nov 07 '16 at 14:16
  • i so tempted to use the easier solution because i understand it , but i understand why it's poor but at the same timer i don't know how to do a delegate handler class for admob ads – Naeim Salib Nov 07 '16 at 14:18
  • can you show me how to ? – Naeim Salib Nov 07 '16 at 14:18
  • Will create a sample project in my free time and share the git link – Abhishek729 Nov 07 '16 at 14:20
  • Meanwhile you can continue working on other aspects of your app – Abhishek729 Nov 07 '16 at 14:21
  • Ok great that you so much and i'll be waiting for your answer – Naeim Salib Nov 07 '16 at 14:26
  • Check the edited answer – Abhishek729 Nov 07 '16 at 14:53
  • ok first of all thank you so much for your help , and one other thing i did what you told me here and in my gameviewcontroller method i called it but nothing showed up and it printed ad wasn't ready , and when i try and call it from my gamescene it gives me an error and higlights the self part and tels me cannot convert value of type 'GameScene ' to expected argument type 'uiviewcontroller' – Naeim Salib Nov 07 '16 at 15:14
  • Should work now. It will print "ad isn't ready". Have patience. It takes a while to cache the ad. I am checking if ad is not ready, I display "ad isn't ready" and then i create ad. – Abhishek729 Nov 07 '16 at 15:54
  • ok so i added currentVc = self admobDelegate.showAd() in the viewdidLoad in my gameviewcontroller and unfortunately nothing happened , and i waited for a fair time for it to load – Naeim Salib Nov 07 '16 at 16:07
  • Sorry but similar code usually works for me. You could put breakpoints and try and see what went wrong. But I guess this gives u a fair understanding of creating delegates and surely you can find the error. Or else share your github link and I may be able to look into it. :-) – Abhishek729 Nov 07 '16 at 16:10
  • sure i will do that thank you , do you want me to share it to you here or send it to you in a private message ? – Naeim Salib Nov 07 '16 at 16:11
  • whatever suits you. :-) – Abhishek729 Nov 07 '16 at 16:12
  • sure here it is https://github.com/naeimsalib/Star-BUst – Naeim Salib Nov 07 '16 at 16:13
  • i've put the addelegate and the gameviewcontroller and the gamescene , because i also want to showad in my game scene – Naeim Salib Nov 07 '16 at 16:14
  • Sorry. My bad. I have made a change in createAd function. interstitialview.delegate = self. Do it and it should work. – Abhishek729 Nov 07 '16 at 16:19
  • no, surprisingly that wasn't it – Naeim Salib Nov 07 '16 at 16:22
  • never mind it worked fine !! – Naeim Salib Nov 07 '16 at 16:24
  • Thank you so much , you are an awesome guy :) – Naeim Salib Nov 07 '16 at 16:25
  • No Probs :-). Recently I wasted 2 weeks trying to integrate ads so i can understand. :-) – Abhishek729 Nov 07 '16 at 16:25
  • But how did it work? – Abhishek729 Nov 07 '16 at 16:26
  • i just added the change in the create ad , but first somehow it didn't work but when i run it again it worked perfectly fine – Naeim Salib Nov 07 '16 at 16:26
  • Cool! All the best. – Abhishek729 Nov 07 '16 at 16:27
  • but actually speaking about ads, i also have a different problem about video based reward ads , and i don't know how to do it , do you happen to know? – Naeim Salib Nov 07 '16 at 16:27
  • U can use unityads for rewarded videos. Very easy to integrate. Just follow the documentation and what u learnt now, you will be fine.If u want rewarded videos in admob, it is bit advanced. It is there in their documentation. You will need to integrate mediation network – Abhishek729 Nov 07 '16 at 16:30
  • or can i just use interstitial ads as a reward based ads , will it be ok or it's better to use rewrd based ads – Naeim Salib Nov 07 '16 at 16:30
  • interstitial ads pay way lesser than rewarded ads – Abhishek729 Nov 07 '16 at 16:30
  • yea i went through it and it is a bit complicated , but anyway that you for your help , and hope we can work together again soon :) – Naeim Salib Nov 07 '16 at 16:31
  • hello again sorry to bother you again , but i was working with unity ad to implement the reward videos , so i downloaded the sdk and bundle .framework and went to my file to implimint the UnityAds but it won't come up and tried removing the sdk and uploading it again but still it didn't work , any idea why ? – Naeim Salib Nov 07 '16 at 18:34