已弃用GKMatchMaker邀请处理程序
我有我以前用来处理邀请的以下代码:
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { // Insert game-specific code here to clean up any game in progress. if (acceptedInvite) { GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite]; mmvc.matchmakerDelegate = self; [self presentViewController:mmvc animated:YES completion:nil]; } };
但是,现在已经在iOS 7
被弃用了。 在哪里以及如何在我的项目中注册GameKit邀请处理程序?
GKInviteEventHandler来救援,并特别看看GKLocalPlayerListener 。
符合GKLocalPlayerListener
协议,你应该没问题。 以下是协议方法,它们看起来是inviteHandler的预期替代,但分为两部分。
- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite - (void)player:(GKPlayer *)player didRequestMatchWithPlayers:(NSArray *)playerIDsToInvite
设置一些对象以符合该对象之后,您只需调用registerListener:
[[GKLocalPlayer localPlayer] registerListener:yourObjectHere]
不要担心尽快注册,因为系统会caching邀请/挑战/回合的内容,如果没有人来处理这些内容,并且只要设置好,就可以让听众知道。
正如Sam所说的那样,新的方法是使用GKLocalPlayerListener协议。 现在这个方法被颠倒了。 在过去,您从部分应用程序向其他玩家发出邀请。 另一部分听取了另一位玩家的邀请,并对此作出了回应。 现在,您可以使用matchMakerViewController 或 Game Center发出邀请(与之前一样),但是现在您可以听取这些邀请的接受了。 之后,Game Center调用didFindMatch来启动一切。 如果您正在收到邀请,Game Center开始您的游戏,然后调用didFindMatch启动它。
这是我的代码:
在我的.h文件GKLocalPlayerListener协议:
@interface MNFStartupViewController : UIViewController<ADBannerViewDelegate, GKMatchmakerViewControllerDelegate, GKMatchDelegate, GKLocalPlayerListener, UIAlertViewDelegate>
在本地玩家进行身份validation之后,在我的authenticateHandler块的.m文件中:
[[GKLocalPlayer localPlayer] registerListener:self];
那么听方法来接受一个邀请:
-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite{ //Called when another player accepts a match invite from the local player. NSLog(@"didAcceptInvite was called: Player: %@ accepted our invitation", player); GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:invite]; mmvc.matchmakerDelegate = self; [self presentViewController:mmvc animated:YES completion:nil];}
现在用Game Center中的一组玩家从Game Center开始游戏的方法。 这很难debugging,因为你不能在Game Center中同时从Xcode运行游戏(我不这么认为),所以有一个debugging的AlertView可以被删除。
-(void)player:(GKPlayer *)player didRequestMatchWithPlayers:(NSArray *)playerIDsToInvite{ //Called when the local player starts a match with another player from Game Center //Start of debugging logging and alerting NSLog(@"In didRequestMatchWithPlayers for players: %@", playerIDsToInvite); NSString *logString = [[NSString alloc] initWithFormat:@"didrequestMatchWithPlayers was called with player IDs: %@", playerIDsToInvite]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logging Alert" message:logString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; //End of debugging logging and alerting //Create a match for the chosen players GKMatchRequest *match = [[GKMatchRequest alloc]init]; match.playersToInvite = playerIDsToInvite; //Create a matchmaking viewcontroller for that match GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc]initWithMatchRequest:match]; mmvc.matchmakerDelegate = self; [self presentViewController:mmvc animated:YES completion:nil];}
这是启动整个配对过程的一种方法:
-(IBAction)setupMatch:(id)sender{ GKMatchmakerViewController *matchViewController = [[GKMatchmakerViewController alloc] initWithMatchRequest:matchRequest]; matchViewController.matchmakerDelegate = self; [self presentViewController:matchViewController animated:YES completion:nil];}
最后,这是Game Center调用的方法,当所有玩家连接并准备就绪时,设置比赛进行。 currentPlayer,currentMatch和hostingPlayer是我自己的属性,具有明显的用途。
-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match{ //Called when GameCenter completes the auto matchmaking process match.delegate = (id)self; [self setCurrentMatch:match]; [self setCurrentPlayers:match.playerIDs]; NSLog(@"Match was found with players: %@, time to get on with the game.", self.currentPlayers); //Use the built in features to decide which device should be the server. self.hostingPlayer = [self chooseHostingPlayerIDfromPlayerIDs:self.currentPlayers]; [self dismissViewControllerAnimated:YES completion:nil];}
希望能帮助到你。
另外,请仅在设备上工作。 我无法邀请在iOS 8.1的模拟器中工作。
我相信GKMatch
和GKTurnBasedMatch
的答案令人讨厌。
对于GKTurnBasedMatch
,邀请会被视为转向事件,并在此function中处理:
player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool)
这在GKLocalPlayerListener
协议中。 您必须正式注册GKLocalPlayerListener
实例与您的本地播放器才能正常工作,因此您只能在validation后执行此操作。
…并不总是工作。 游戏中心是不可靠的。 但有时它确实有效,那是…什么?