Game Center inviting friends programmatically

2019-04-09 15:51发布

问题:

I'm facing difficulty inviting a friend to the match.

GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; 
    request.minPlayers = 2;
    request.maxPlayers = 2;
    request.playersToInvite = [NSArray arrayWithObjects: @"G:1102359306",nil ];


//  GKMatchmakerViewController *mv = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
//  [self presentModalViewController:mv animated:YES];


    [[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch *match, NSError *error) {
        if (error) {
            NSLog([error description]);
        }
        else if (match != nil) {NSLog(@"good match");
            //self.chatMatch = match;
            //self.chatMatch.delegate = self;       
            //[self chatReady];
        }
        else {
            NSLog(@"other error");
        }

    }];

The problem is I never receive the invitation notification on second device logged to the account - G:1102359306. When I use GKMatchmakerViewController (uncomment above 2 lines) and comment GKMatchmaker block I have automatically checked the good friend - G:1102359306 and when I invites him the notification with accept/decline is shown, that's how I know it's correct.

Do you see anything wrong with the code above? I want to use my own UI to handle the multiplayer mode. The strange problem is I also don't see in console any logs good match/other error, and the [error description] is only printed when I call the above code twice - it says that the previous req was canceled.

回答1:

You cannot programmatically invite a specific set of players to a match. The findMatchForRequest:withCompletionHandler: documentation says this:

The match request’s playersToInvite property is ignored; to invite a specific set of players to the match, you must display a matchmaker view controller.

There is no public API that does what you want.