Some startBrowsingForNearbyPlayersWithReachableHandler questions

后端 未结 3 474
余生分开走
余生分开走 2021-01-13 11:22

I\'m trying to get local matchmaking working in GameKit using [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:]. Essentially,

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 11:55

    You need to do these things in this order:

    1. Authenticate the local player
    2. Install an invite handler
    3. Start browsing for nearby players

    Authentication is required - this registers your app with Game Center and logs the player in. In most cases, you won't even need internet access to do this.

    Installing the invitation handler is also required, and I think this is the step you're missing. This lets your app know what to do when an inbound invitation is received. If you don't do this, a device won't register as being nearby.

    Only start browsing once you've done the above two.

    Here's some sample code to get you going. Call this method after the app launches:

    - (void) authenticateLocalPlayer
    {
    
        static BOOL gcAuthenticationCalled = NO;
        if (!gcAuthenticationCalled) {
            GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    
            void (^authenticationHandler)(UIViewController*, NSError*) = ^(UIViewController *viewController, NSError *error) {
                NSLog(@"Authenticating with Game Center.");
                GKLocalPlayer *myLocalPlayer = [GKLocalPlayer localPlayer];
                if (viewController != nil)
                {
                    NSLog(@"Not authenticated - storing view controller.");
                    self.authenticationController = viewController;
                }
                else if ([myLocalPlayer isAuthenticated])
                {
                    NSLog(@"Player is authenticated!");
    
                    //iOS8 - register as a listener
                    [localPlayer unregisterAllListeners];
                    [localPlayer registerListener:self];
    
                    [[GKLocalPlayer localPlayer] loadFriendPlayersWithCompletionHandler:^(NSArray *friendPlayers, NSError *error) {
    
                        //Do something with the friends
    
                    }];
    
                    //iOS7 - install an invitation handler
                    [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
                        // Insert game-specific code here to clean up any game in progress.
                        if (acceptedInvite)
                        {
                            //This player accepted an invitation.
                            //If doing programmatic matchmaking, call GKMatchmaker's matchForInvite:completionHandler 
                            //to get a match for the invite.  Otherwise you need to allocate a GKMatchmakerViewController 
                            //instance and present it with the invite.
    
                        }
                        else if (playersToInvite)
                        {
                            //Your game was launched from the GameCenter app to host a match.
                        }
                    };
    
                    //Now you can browse.  Note this is the iOS8 call.  The iOS7 call is slightly different.
                    [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithHandler:^(GKPlayer *player, BOOL reachable) {
    
                        NSLog(@"Player Nearby: %@", player.playerID);
    
                    }];
    
    
    
                }
                else
                {
                    //Authentication failed.
                    self.authenticationController = nil;
                    if (error) {
                        NSLog([error description], nil);
                    }
                }
    
    
            };
    
            localPlayer.authenticateHandler = authenticationHandler;
            gcAuthenticationCalled = YES;
        }
    }
    

    * IMPORTANT * If you're using iOS8, you don't install the invitation handler. You instead register an object as listening for the protocol GKLocalPlayerListener, and implement these methods:

    -player:didAcceptInvite:
    -player:didRequestMatchWithRecipients:
    

    If you don't implement these methods on iOS8, it won't work!

    You then link GKMatchmaker to that object by calling this after authenticating the local player:

    [localPlayer registerListener:self];
    

    Make sure the object that's implementing the protocol is declared like so in the .h file:

    @interface MyImplementingObject : NSObject 
    

    If you do all this and it still isn't working, make sure that you have your bundle ID set correctly in your app (Click the app, click 'Targets', make sure Bundle Identifier and Version are filled in), then click the 'Capabilities' tab (XCode 6), and make sure Game Center is on.

    Go to the Member Center and make sure that the app using that bundle ID also has Game Center enabled for its Provisioning Profile. Download and reapply your Provisioning Profile if necessary.

    Make sure the sandbox switch is ON in your Settings under GameCenter, and also make sure that 'Allow Invites' and 'Nearby Players' switches are turned ON.

    Finally, make sure you go to iTunes Connect and verify that Game Center is enabled for your app there as well.

提交回复
热议问题