Some startBrowsingForNearbyPlayersWithReachableHandler questions

后端 未结 3 473
余生分开走
余生分开走 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 <GKMatchDelegate, GKLocalPlayerListener>
    

    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.

    0 讨论(0)
  • 2021-01-13 11:57

    So, keep in mind the differences between iOS7 and iOS8. This code will work on either version and call 'updateNearbyPlayer' in turn.

    if ( IS_IOS8 )
    {
        [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithHandler:^(GKPlayer *gkPlayer, BOOL reachable) 
         {
             NSLog(@"PLAYER ID %@ is %@",gkPlayer.playerID,reachable?@"reachable" : @"not reachable");
             [self updateNearbyPlayer:gkPlayer reachable:reachable];
         }];
    
    } else {
        /*
         *  iOS 7...
         */
        [[GKMatchmaker sharedMatchmaker] startBrowsingForNearbyPlayersWithReachableHandler:^(NSString *playerID, BOOL reachable) 
         {
             NSLog(@"PLAYER ID %@ is %@",playerID,reachable?@"reachable" : @"not reachable");
    
             [GKPlayer loadPlayersForIdentifiers:@[playerID] withCompletionHandler:^(NSArray *players, NSError *error) {
                 NSLog(@"Loaded: %@, error= %@",players,error);
                 GKPlayer *gkPlayer = [players objectAtIndex:0];
                 [self updateNearbyPlayer:gkPlayer reachable:reachable];
             }];
         }];
    }
    

    With some delay due to the underlying Bonjour services, this mechanism works great. However, it needs to be balanced with an appropriate call to:

    [[GKMatchmaker sharedMatchmaker] stopBrowsingForNearbyPlayers];
    

    Whenever you use one of the GKPlayers/PlayerIDs reported to establish a match or to add it to an existing match, you should stop browsing. Once the match has been finished, closed or cancelled, start browsing again. Otherwise, on the second attempt to connect to a nearby device, you'll fail.

    0 讨论(0)
  • 2021-01-13 12:04

    1) Am I right in thinking I can call startBrowsingForNearbyPlayersWithReachableHandler: before authenticating the local player?

    No. startBrowsingForNearbyPlayersWithReachableHandler works by both advertising the existing player and browsing for other players but, most importantly, the information it uses to identify players is the playerID... which won't be available until the player authenticates.

    3) There seem to be conflicting reports on the web over whether GameKit-enabled apps can be tested in the iOS Simulator. General consensus seems not, and it's better to test on iOS hardware

    GameCenter authentication, achievements, and leaderboards work in the simulator, everything else should be tested on real hardware. I actually recommend the simulator for authentication testing, since it avoids the sandbox/production switch which can make detailed auth testing on devices somewhat confusing. Everything else can only be tested on devices. The simulator doesn't have great support for receiving push notifications which breaks match setup and the general hardware configuration is different enough that match communication isn't unlikely to work right anyway.

    0 讨论(0)
提交回复
热议问题