Reputation: 1332
From what I understand, the UIViewController manages interactions with the interface, such as transitions, while a View is just an object, a vessel for context. Therefore, I am confused on how to get the UIViewController of an SKScene
I am trying to implement Google Mobile Ads into my project:
interstitial.present(fromRootViewController: /* UIViewController */)
However, one of the arguments requires an UIViewController object. I have looked at some other different questions, but most were a bit outdated and in Objective-c. Does anyone have any suggestions on the best way to get the UIViewController of an SKScene
This is the method where I am initializing the interstitial:
override func didMove(to view: SKView) {
interstitial = GADInterstitial(adUnitID: "code")
let request = GADRequest()
interstitial.load(request)
if interstitial.isReady {
// interstitial.present(fromRootViewController: /* UIViewController */)
} else {
print("Ad wasn't ready")
}
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.backgroundColor = UIColor(red:0.17, green:0.24, blue:0.31, alpha:1.0)
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
}
Upvotes: 2
Views: 1293
Reputation: 11
It says it that
…skView.presentScene(scene)
scene.viewController = self
SKView can’t work and neither will skView. PresentScene can’t be used on type SKView
Upvotes: 0
Reputation: 2832
Your view controller is:
self.view?.window?.rootViewController
Example:
let isReady = GADRewardBasedVideoAd.sharedInstance().isReady
guard let controller = self.view?.window?.rootViewController as? GameViewController else {return}
if isReady {
print("ADMOB: started")
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: controller)
}
Upvotes: 3
Reputation: 4066
Declare the VC on your skscene and set it to self on your View Controller. Something like this:
SKScene
var viewController: myViewController!
override func didMove(to view: SKView) {
viewController.myVariable = 10
}
View Controller
class myViewController: UIViewController, SKSceneDelegate {
var scene: GameScene!
var myVariable: Int
override func viewDidLoad() {
super.viewDidLoad()
skView.presentScene(scene)
scene.viewController = self
}
Upvotes: 2