GKMatchMaker邀请处理程序已弃用

时间:2013-12-13 12:56:50

标签: ios objective-c game-center gamekit

我之前使用以下代码来处理邀请:

[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邀请处理程序?

4 个答案:

答案 0 :(得分:5)

GKInviteEventHandler救援,特别是GKLocalPlayerListener

符合GKLocalPlayerListener协议,您应该没问题。下面是协议方法,它们看起来是invitationHandler的预期替代品,但分为两部分。

- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
- (void)player:(GKPlayer *)player didRequestMatchWithPlayers:(NSArray *)playerIDsToInvite

设置一些符合要求的对象后,您只需拨打registerListener:

[[GKLocalPlayer localPlayer] registerListener:yourObjectHere]
不要担心会尽快注册,因为系统会根据邀请/挑战/回合的内容来缓存,如果没有人处理这些内容并让你的听众知道你的话设置它。

答案 1 :(得分:4)

Sam说,新的方法是使用GKLocalPlayerListener协议。这种方法现在已经逆转。过去,您通过部分应用向其他玩家发出了邀请。另一部分听取了另一位玩家的邀请并对此作出回应。现在,您使用matchMakerViewController 游戏中心发出邀请(如前所述),但现在您要听取这些邀请的接受。之后,Game Center调用didFindMatch来启动所有操作。如果您收到邀请,Game Center将启动您的游戏,然后调用didFindMatch启动它。

这是我的代码:

在我的.h文件中使用GKLocalPlayerListener协议:

@interface MNFStartupViewController : UIViewController<ADBannerViewDelegate, GKMatchmakerViewControllerDelegate, GKMatchDelegate, GKLocalPlayerListener, UIAlertViewDelegate>
在本地播放器通过身份验证后,在我的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中选择了一组玩家。这很难调试,因为你无法在同时从Xcode运行游戏时在游戏中心启动游戏(我不这么认为!)所以有一个可以删除的调试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];}

希望它有所帮助。

答案 2 :(得分:3)

此外,邀请仅在设备上工作。我无法邀请在iOS 8.1的模拟器中工作。

答案 3 :(得分:1)

我认为,对于GKMatchGKTurnBasedMatch,答案很烦人。

对于GKTurnBasedMatch,邀请计为转弯事件,并在此功能中处理:

player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool)

这是GKLocalPlayerListener协议中的内容。您必须在本地播放器上正式注册GKLocalPlayerListener实例才能使用此功能,因此您只能在身份验证后执行此操作。

...并且它并不总是有效。 游戏中心不可靠。但它确实有效,那就是......某事?