0

Very confused about an issue I've just stumbled across. I have a swift game, mainly using SpriteKit. At the end of a game an ad is loaded. Or when the user decides to end the game an ad is also loaded.

If the game is played normally. The flow is as follows...

MenuScene -> GameScene -> [ad loads] -> GameOverScene

If the user quits the game...

MenuScene -> GameScene -> user selects pause -> user clicks quit -> [ad loads] -> Menu Scene

Both scenarios work perfectly for iOS 13

However, in iOS 12, only the user quitting mid game works, in the other scenario the app freezes (no obvious errors to me in the log)

Ending a game, via either method, calls the exact same function. Which uses a notification to load the ad.

The following code is all in the GameViewController. And the notification is posted in the GameScene.

NotificationCenter.default.addObserver(self, selector: #selector(GameViewController.showAd), name: NSNotification.Name(rawValue: "loadAndShow"), object: nil)

An ad is prepared in ViewDidLoad()

myAd = createAd()

And the function is called by the Notification

@objc func showAd() {
    if (myAd.isReady) {
        myAd.present(fromRootViewController: self)
        myAd = createAd()
    }
}

func createAd() -> GADInterstitial {

    myAd = GADInterstitial(adUnitID: "MY ADMOB ID")
    let request = GADRequest()
    GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = ["MY DEVICE ID"]
    myAd.delegate = self
    myAd.load(request)
    return myAd

}

Any help would be amazing

UPDATE

Have found a potential workaround. Am now presenting the ad in a different thread and no longer experiencing any freezing.

        DispatchQueue.main.async {
            self.myAd.present(fromRootViewController: self)
        }

Can only assume iOS13 has better handling for this. And the reason the "game quit" scenario worked was because it was transitioning to the menu scene which is also the GameViewController. The app didn't like presenting an ad from the GameViewController while also transitioning to a new scene.

jdez
  • 57
  • 1
  • 7
  • Check if your project's scheme has the "main thread checker" turned off. iOS has always required that UIKit methods can only be called on the main thread. – Gereon May 09 '20 at 12:48
  • main thread checker is enabled – jdez May 09 '20 at 17:01

0 Answers0