I am using two real iPhones (A and B) to test game center "Invite" logic and I am getting confused.
I use iPhone-A to start my game and then launch the GKMatchmakerViewController. I then select the "Invite Friends" button and select a friend to invite (which is iPhone-B).
On iPhone-B, I receive a popup message asking whether I want to join the game. After I click the popup message, I see my game is launched on iPhone-B. At this point, I assume the player( player: didAccept invite:) function would be called on iPhone-B automatically and then a GKMatchmakerViewController should be created by providing the GKInvite object by calling the function below:
let mmvc = GKMatchmakerViewController(invite: inviteToAccept)
mmvc!.matchmakerDelegate = self
viewController.present(mmvc!, animated: true, completion: nil)
Where does the "viewController" comes from to allow me to present/display the GKMatchmakerViewController?
Can someone explain how they present the GKMatchmakerViewController when the didAccept function is called?
In Objective C:
- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite{
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:invite];
mmvc.matchmakerDelegate = self;
[[self topController] presentViewController:mmvc animated:YES completion:^{
mmvc.view.frame=CGRectMake(0,mmvc.view.safeAreaInsets.top, mmvc.view.bounds.size.width, mmvc.view.bounds.size.height-mmvc.view.safeAreaInsets.top);
}];
}
-(UIViewController *)topController{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (true){
if (topController.presentedViewController) {
topController = topController.presentedViewController;
}else if([topController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)topController;
topController = nav.topViewController;
}else if([topController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tab = (UITabBarController *)topController;
topController = tab.selectedViewController;
}else{
break;
}
}
return topController;
}