Reputation: 5
This the code block I'm having trouble with.It gives me error because of 'presentViewController'
func authenticateLocalPlayer(){
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
}
else {
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
I know it's because SKScene has already has a UIViewController, but I don't know how else to represent it.
Upvotes: 0
Views: 225
Reputation: 105
There is simple way to deal with it within the scene. Try it and let us know. I don't disagree with answer above completely, but they should have guided you better and given the right answer. Yes You have to call presentViewController on a UIViewController. Since your SKScene is using SKView which is subclass of View but not UIView at all, so calling presentViewController from self means you are asking SKView object to present a UIViewController which SKView cannot. But here is a simple solution, as SKView is subclass of UIView itself, so ask SKView's root view controller to do this for you like this. Your call will be like this (I am writing it in objective C as I do not code in swift but same is true for both languages)
[self.view.window.rootViewController presentViewController:viewController animated:YES completion:nil];
As you see instead of asking 'self', you have to ask 'self.view.window.rootViewController'. And you can do it right inside your scene. There you go.
Upvotes: 3
Reputation: 80781
You can't call presentViewController
from within your SKScene
, you need to call it on a UIViewController
.
Therefore you want to find the parent view controller that contains your Sprite Kit content and call the function there.
Upvotes: 0