如何使用游戏中心向其他玩家发送邀请?

时间:2015-02-27 15:26:09

标签: objective-c game-center

我是游戏中心的新手,我正在构建多人游戏。我的tableview中有不同的用户;现在我想向特定用户发送匹配邀请。要发送邀请,我使用此代码:

GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;

request.playersToInvite = [[NSArray alloc] initWithObjects:player.playerId, nil];
request.inviteMessage = @"Your Custom Invitation Message Here";

request.inviteeResponseHandler = ^(NSString *playerID, GKInviteeResponse response)
{
    [self updateUIForPlayer: playerID accepted: (response == GKInviteeResponseAccepted)];
};

但是如何收到邀请?我想我必须在GameKitHelper类中实现这个方法,但我不知道如何处理它。

1 个答案:

答案 0 :(得分:3)

我的老师帮助了我,我想在这里发布工作代码,如果有人卡在这里可以使用这段代码。

- (void)inviteFriends: (NSArray*) friends
{
       GKMatchRequest *request = [[GKMatchRequest alloc] init];
    request.minPlayers = 2;
    request.maxPlayers = 2;
    request.defaultNumberOfPlayers = 2;
    request.recipients =  friends;
    request.inviteMessage = @"Your Custom Invitation Message Here";
    [[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch* match, NSError *error) {
        if (error)
        {
            //Invite has not been sent

            //  //NSLog(@"Invitation has not been sent");
        }
        else if (match != nil)
        {
            //whatever you want to do when the receiver accepts the invite
            //NSLog(@"Invitation has  been sent with match object = %@",match);

        }
    }];
    request.recipientResponseHandler= ^(GKPlayer *player, GKInviteeResponse response)
    {

        //NSLog(@"response Get From Other User.");

        switch (response) {
            case GKInviteeResponseAccepted:
            {

            }
                break;
            case GKInviteeResponseDeclined:
            {

            }
                break;
            case GKInviteeResponseFailed:
            {

            }
                break;
            case GKInviteeResponseIncompatible:
            {

            }
                break;
            case GKInviteeResponseUnableToConnect:
            {


            }
                break;
            case GKInviteeResponseNoAnswer:
            {


            }
                break;

            default:
                break;
        }

    };


}