In objC the syntax written by Rawendrich for GKTurnBasedEventListener
, which was GKTurnBasedEventHandler
there at that time, now changed by Apple is as below.
if (!gameCenterAvailable) return;
void (^setGKEventHandlerDelegate)(NSError *) = ^ (NSError *error)
{
GKTurnBasedEventHandler *ev =
[GKTurnBasedEventHandler sharedTurnBasedEventHandler];
ev.delegate = self;
};
NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer]
authenticateWithCompletionHandler:
setGKEventHandlerDelegate];
} else {
NSLog(@"Already authenticated!");
setGKEventHandlerDelegate(nil);
}
Now after converting this to swift, and with the composition of writing down GKTurnBasedEventListener
instead of GKTurnBasedEventHandler
, this comes the following way.
// Converted with Swiftify v1.0.6381 - https://objectivec2swift.com/
if !gameCenterAvailable {
return
}
var setGKEventHandlerDelegate: ((_: Error) -> Void)? = {(_ error: Error?) -> Void in
var ev = GKTurnBasedEventHandler.shared()
ev.delegate = self
}
print("Authenticating local user...")
if GKLocalPlayer.localPlayer().authenticated == false {
GKLocalPlayer.localPlayer().authenticate(withCompletionHandler: setGKEventHandlerDelegate)
}
else {
print("Already authenticated!")
setGKEventHandlerDelegate(nil)
}
Unfortunately this is not the right syntax to set delegate of GKTurnBasedEventListener
for my ViewController
.
Please if anyone of you could solve this for me, because without this I'm not able to read through event listener's default functions.
Cheers!
FYI, if you want a working example of how to use GKLocalPlayerListener during a turn-based GameKit match, you are welcome to take a look at this example project for a turn based game. I hope it helps to see all the pieces in context.
Finally after just about harsh 10 hours, I figured out this problem from Here. Although this syntax is in objC, but there's no problem of converting it to swift from Swiftify.
Although a bit later than the real time, but I'm now able to understand that setting delegate of
GKTunBasedEventListener
is not like the one we do forUITableViewControllerDelegate
.Here, one must have to first authenticate local player, then after you have to register local player's listener to the
ViewController
'sdelegate
GKLocalPlayerListener.One other thing I found on Apple's Documentation: Do not implement
GKChallengeListener
,GKInviteEventListener
,GKSavedGameListener
, andGKTurnBasedEventListener
directly; implementGKLocalPlayerListener
instead. You can listen for and handle multiple events usingGKLocalPlayerListener
.So then on I've implemented in the following way.
Now in
ViewDidLoad()
function put the following code.And finally your Authentication function should be like this.
NOTE:
GKLocalPlayerListener
won't work on simulator.