我有以下代码,用于处理邀请:
[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邀请处理程序?
发布于 2013-12-13 13:55:24
GKInviteEventHandler来拯救,特别是看看GKLocalPlayerListener。
遵循GKLocalPlayerListener协议,您应该没事。下面是协议方法,这些方法看起来是对invitationHandler的预期替代,但分为两部分。
- (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite
- (void)player:(GKPlayer *)player didRequestMatchWithPlayers:(NSArray *)playerIDsToInvite在设置了一些对象以符合这一要求之后,只需调用registerListener:即可。
[[GKLocalPlayer localPlayer] registerListener:yourObjectHere]不要担心尽快注册它,因为系统缓存邀请/挑战/基于转弯的东西,如果没有人来处理,并且让你的听众知道你的设置。
发布于 2014-07-23 11:50:16
正如萨姆所说,新的方法是使用GKLocalPlayerListener协议。现在,这一做法发生了逆转。在过去,你会从你的应用程序中向其他玩家发出邀请。另一部分则听取了另一名球员的邀请,并对此作出了回应。现在,您可以使用matchMakerViewController 或游戏中心发出邀请(与以前一样),但现在您可以收听这些邀请的接受情况。在那之后,游戏中心打电话给didFindMatch,让一切都开始。如果您收到邀请,游戏中心启动您的游戏,然后打电话给didFindMatch启动它。
这是我的密码:
在我的.h文件中,GKLocalPlayerListener协议:
@interface MNFStartupViewController : UIViewController<ADBannerViewDelegate, GKMatchmakerViewControllerDelegate, GKMatchDelegate, GKLocalPlayerListener, UIAlertViewDelegate>在本地播放机通过身份验证后,在我的.m块中的authenticateHandler文件中:
[[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];}现在,从游戏中心开始游戏的方法是在游戏中心选择一组玩家。这是很难调试的,因为您不能在游戏中心同时从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];}最后,这是一种由游戏中心调用的方法,当所有玩家连接好并准备就绪时,就可以设置比赛进行。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];}希望能帮上忙。
发布于 2014-11-06 18:46:12
此外,只邀请在设备上工作。在iOS 8.1中,我无法获得在模拟器工作的邀请。
https://stackoverflow.com/questions/20567104
复制相似问题