4

I've been looking all over for an answer to this and I've found lots of examples in objective C (google developer docs, etc.) and some answers in swift, but not using spritekit, and being a novice, I just haven't been able to bridge the gaps in these tutorials to put it all together for my project. Honestly, I don't really care if the ad is called on app launch, or game over, I'd just be happy being able to call the ad period, though I guess on game launch is preferred.

Pretty sure I've got it all set up correctly in gameviewcontroller.swift, but I don't know how to actually call it. Here is the code from my gameviewcontroller.swift:

import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController {

    var interstitial : GADInterstitial!

 func createAndLoadAd() -> GADInterstitial {

        var ad = GADInterstitial()
        ad.adUnitID = "ca-app-pub-4471013071748494/2980967718"
        var request = GADRequest()
        request.testDevices = [""]

        return ad

    }

Beyond this ... I also have this if statement to call the ad, but I don't know where it should be ... or if it's complete (in the tutorial, it was in a button, but I need it to be automatic, naturally:

if (self.interstitial.isReady) {
            self.interstitial.presentFromRootViewController(self)
            self.interstitial = self.createAndLoadAd()
        }

Anyone have any experience with this? If anyone could help me complete this by letting me know where to place that above if statement, or if there is more that needs to be done ... your help will be much appreciated. Thank you.

user3147770
  • 203
  • 4
  • 16
  • 1
    I think that when you present it you are killing it by calling create method again - you should call that method when your ad is dissmised. I maybe wrong but for me it worked the only difference I ise obj-c – Coldsteel48 Apr 24 '15 at 03:45
  • 1
    This if dtatement is what actually calling the ad . put it whereever you want the ad to popup , lets say in game over or a viewDidLoad it is up to you – Coldsteel48 Apr 24 '15 at 03:47
  • Better call it on game over because the ad has to load(it takes time depends on connection speed) – Coldsteel48 Apr 24 '15 at 03:48
  • Btw in your code there is missing Load Request method for ad – Coldsteel48 Apr 24 '15 at 03:50
  • Thanks ... but I don't quite understand what you're saying in the first and last comments. Not sure I'm killing it when I present it ... the code above is straight from a tutorial that put an ad in a button, dude pressed the button and it worked. I just want it to be automatic or event based instead of button based. But, this is on my gameviewcontroller.swift ... how can I call it from gamescene.swift or gameend.swift (the two swift files that create the visual aspects of the game) ?? Can you offer some sample code by chance?? – user3147770 Apr 24 '15 at 11:24
  • Oh, and yes ... the missing load request method ... I think that might be what I'm asking how to do/where to place ... beyond that if statement. The google developer docs are only in objective C ... but even if they had some docs for swift, I doubt it would be for a spritekit environment. Derp! This is the last thing I needed to do to complete this app ... wish I could go ahead and put a bounty on this. ;-) – user3147770 Apr 24 '15 at 14:33
  • You can add the ads as a UIKit on top of the SpriteKit - it shouldn't be an issue at all . I cant offer you a sample code ... I am not messing with swift , all i can tell is that in objective-c you should call [request loadRequest]; as the last line in initialization of the ad ... Just look at the header of the GadTequest and find the parallel method – Coldsteel48 Apr 24 '15 at 17:22
  • Thanks! That's what's frustrating "It shouldn't be an issue at all" doh! Will look into the call request line/header of the GADRequest and see what I can figure out. – user3147770 Apr 24 '15 at 17:50
  • You have a viewController right ? ... on view controller u r presenting scene right ? now u have to init and load ads on view controller(not the scene) and from the scene to delegate to viewController to present the ad (presentation is the first line in ur if statement) – Coldsteel48 Apr 24 '15 at 17:51
  • I am sorry for my bad english , I just dont know how to explain it better – Coldsteel48 Apr 24 '15 at 17:53
  • No, not really. It's not like other apps I've made where you have a regular view controller and storyboard. This spritekit set up has the gameviewcontroller.swift that I never coded anything on until implementing this ad, and it has gamescene.swift that I didn't do any visual stuff on, but where I coded all the visual objects for the game programmatically, and an endscene.swift I added to code a high score and restart button, also programmatically. Sorry my programming vocabulary is so limited- words like initialization throw me for a loop. I'm a novice, derp. Thank you though!!! – user3147770 Apr 24 '15 at 19:18
  • Incidentally, I do have an admob banner ad implemented and working just fine. It was also coded on the gameviewcontroller.swift, and I didn't need to ad any code to the gamescene.swift to make it work. No idea why it was simple to grasp and this interstitial ad is just breaking my head. Thank you for all your help, though. Much appreciated! – user3147770 Apr 24 '15 at 19:26
  • it is the same !add it in the view controller , yes programmatically all my 8 apps works programmatically ... – Coldsteel48 Apr 24 '15 at 20:24
  • If you want you can mail me ur project and i will add ot there ... – Coldsteel48 Apr 24 '15 at 20:25
  • In my best Chris Griffin voice, "Whaaaaaaat?" :-) Wait, but it's in swift ... thought you said you didn't do the whole swift thing ??? Thanks man ... currently adding a timer to the banner ad ... will have to figure out how to export the project to email it too. – user3147770 Apr 24 '15 at 20:56
  • swift or not it is the same hierarchy and same things – Coldsteel48 Apr 24 '15 at 21:18
  • Okay I will make you an example in couple of hours (in swift) ... I understand that u r furstrated (btw I meant that u will send only the viewController part) ... Anyway I will make u an answer in 2-3 hours with an example in swift – Coldsteel48 Apr 24 '15 at 21:24
  • What's your email? Checked your profile page and didn't see it? I can quickly and easily just copy/paste the gameviewcontroller.swift code. :-) – user3147770 Apr 24 '15 at 21:37
  • OH ... and if I haven't said it enough -- thank you!!! – user3147770 Apr 24 '15 at 21:37
  • I made you an Example, Hope it will help. (BTW change the ADUnit ID , it is from my app.) – Coldsteel48 Apr 24 '15 at 21:50

3 Answers3

9

Ok so I started a new project (Swift project) which is Game Project

In GameViewController:

class GameViewController: UIViewController
{
var interstitial = GADInterstitial()

override func viewDidLoad()
{
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene
    {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)



        let delayTime = dispatch_time(DISPATCH_TIME_NOW,
            Int64(5 * Double(NSEC_PER_SEC)))

        //Creation of the ad and its request
        self.interstitial = GADInterstitial()
        self.interstitial.adUnitID = "ca-app-pub-4798156420380453/7714267723"
        var request = GADRequest()
        // request.testDevices = [""]
        self.interstitial.loadRequest(GADRequest());//The method u were missing

        dispatch_after(delayTime, dispatch_get_main_queue())
        {
            self.showAd()
        }

    }
}



 func showAd()
 {
    if (self.interstitial.isReady)
    {
        self.interstitial.presentFromRootViewController(self)//Whatever  shows the ad
    }
}

It is my first swift code so don't judge me if I did something wrong But when I lunch it after ±5 seconds i see the ad Here is your example.

I am not familiar with Swift at all , but all you need to do is to get the interstitial delegate when it dismissed -> and request for the next interstitial, So in next time you will get the new AD

Feel free to ask anything ! P.S. Don't forget to change the admob ID to your ID.

Coldsteel48
  • 3,482
  • 4
  • 26
  • 43
  • Fantastic!! Awesome, thank you!!! Got it working. Sorry for the long delay in response, had to put in some family time before getting back to it. This is awesome. Nice work on the dispatch. Excellent! Now I can play with this some, see about maybe only getting it to show on the game over screen or something ... but I won't trouble you no more, you have definitely gone above and beyond. thankYou x10^123!!! – user3147770 Apr 25 '15 at 04:45
  • Hmm, ok, been sitting here trying different things and the words "Feel free to ask anything" keep coming up in my head. So ... reckon I'll ask (feel free to ignore, fair?) ... What would you do different if you wanted to call the add on a change of scene instead of after the delayTime ? This app goes from gamescene to endscene when the player dies ... it would be awesome if the ad came up right when the endscene loaded. Any thoughts? No worries either way -- seriously, feel free to ignore, I'll keep at it. :-) – user3147770 Apr 25 '15 at 05:29
  • Well in obj-c the aproach is to delegate the showAd method from the endScene to viewController (can also use nsnotification center) ot should be the same in swift just Idk the syntax of how to declare it, i will try to look ot up the net – Coldsteel48 Apr 25 '15 at 12:06
  • Here the docs. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html – Coldsteel48 Apr 25 '15 at 12:07
  • here is the notification center example http://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/ – Coldsteel48 Apr 25 '15 at 12:09
  • The fastest way for you is to implement the notification center , but in general u must learn the delegate pattern. – Coldsteel48 Apr 25 '15 at 12:10
  • Thank you very much. Yes, I want to learn how to do it all. Maybe one day I can be as helpful to some one else as you and several other awesome peeps on this site have been to me. Super thanks, amigo!!! – user3147770 Apr 25 '15 at 16:20
3

My solution based on above comments was the following:

1) Add this in the GameViewController.swift

protocol AdmobInterstitialDelegate{
    func showInterstitial()
}
...
// add this delegate in GameViewController definition
class GameViewController: UIViewController, GADInterstitialDelegate, AdmobInterstitialDelegate {
...
// and assign it to the GameScene class when you create its instance
     let scene = GameScene()
     scene.adDelegate = self
// do not forget to implement the delegated method itself
    func showInterstitial(){
        if (interstitial!.isReady) {
            interstitial!.presentFromRootViewController(self)             
        }
    }

and then you can use it in GameScene

class GameScene: SKScene {
....
    var adDelegate: AdmobInterstitialDelegate?
....
// and finally - show the ad
    override func didMoveToView(view: SKView) {
        self.adDelegate?.showInterstitial();
        ....

hope this helps.

Piotr
  • 1,597
  • 3
  • 18
  • 25
1

keep the if condition in viewDidAppear it will automatically execute

override func viewDidAppear(animated: Bool) {
        if (interstitial.isReady) {
        interstitial.presentFromRootViewController(self)
        self.interstitial = self.createAndLoadInterstitial()
        }
Sajjad Aims
  • 99
  • 3
  • 12