Load and Show an MPInterstitialAdController in iOS SpriteKit


#1

Hello,

I’m having an issue with MoPub in Spritekit that I haven’t had with other apps that are not games. The issue is my view controller is called gameviewcontroller.swift and opening it asks you to login to game center or it welcomes you back from game center, however since adding the interstitial ad it neither brings game center back, nor load the interstitial ad, despite completing the on boarding process of MoPub. Below is the code. Any feedback is helpful.

Thanks!

David

import UIKit
import SpriteKit
import GameKit
import MoPub

extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file as String, ofType: “sks”) {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
        archiver.finishDecoding()
        return scene
    } else {
        return nil
    }
}

}

class GameViewController: UIViewController , MPInterstitialAdControllerDelegate {

// TODO: Replace this test id with your personal ad unit id
var interstitial: MPInterstitialAdController =
MPInterstitialAdController(forAdUnitId: "77ce0b65cf81438eb255695afe3b1904")


override func viewDidLoad() {
    
    self.interstitial.delegate = self
    // Pre-fetch the ad up front
    self.interstitial.loadAd()

    func interstitialDidLoadAd(interstitial: MPInterstitialAdController) {
        // This sample automatically shows the ad as soon as it's loaded, but
        // you can move this showFromViewController call to a time more
        // appropriate for your app.
        if (interstitial.ready) {
            interstitial.showFromViewController(self)
        }
    }

    
    NSNotificationCenter.defaultCenter().addObserver(self, selector:
        Selector("showAuthenticationViewController"), name:
        PresentAuthenticationViewController, object: nil)
    
    GameKitHelper.sharedInstance.authenticateLocalPlayer()
    super.viewDidLoad()
}

func showAuthenticationViewController() {
    let gameKitHelper = GameKitHelper.sharedInstance
    
    if let authenticationViewController = gameKitHelper.authenticationViewController {
        presentViewController(authenticationViewController, animated: true,
            completion: nil)
    }
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}





override func viewDidLayoutSubviews() {
    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = false
        skView.showsNodeCount = false
        skView.showsDrawCount = false
        skView.showsPhysics = false

        
        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = false
        
        /* Set the scale mode to scale to fit the window */
        /* 
         Adjusted for iOS8 
        */
        scene.scaleMode = .ResizeFill
        scene.size = skView.bounds.size
        
        skView.presentScene(scene)
    }
}


override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

}


#2

Hi @graphicpeace,

Thanks for reaching out! Could you please reach out to support@mopub.com with device logs from your testing so we can help you look into this?

Thank you!