iOS 6 Game Center authenticateHandler can't login after a cancel

后端 未结 2 623
陌清茗
陌清茗 2020-12-15 17:08

When using the authenticateHandler in iOS 6, game center won\'t present the login view if the user cancels it. I realize game center will auto lockout an app after 3 cancel

2条回答
  •  半阙折子戏
    2020-12-15 17:27

    Better use runtime checks (instancesRespondToSelector:) instead of preprocessor #if statements, so that you can use recommended methods where they are available and depreciated ones elsewhere. I actually found I need to distinguish three cases before setting the invite handler, as the authentication handler might also get called with a nil view controller:

     -(void)authenticateLocalPlayer
     {
         if ([[GKLocalPlayer class] instancesRespondToSelector:@selector(setAuthenticateHandler:)]) {
             [[GKLocalPlayer localPlayer] setAuthenticateHandler:^(UIViewController *gameCenterLoginViewController, NSError *error) {
                 if (gameCenterLoginViewController) {
                     [self.presentedViewController presentViewController:gameCenterLoginViewController
                                                                animated:YES
                                                              completion:^{
                                                                  [self setInviteHandlerIfAuthenticated];
                                                              }];
                 } else {
                     [self setInviteHandlerIfAuthenticated];
                 }
             }];
         } else { // alternative for iOS < 6
             [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
                 [self setInviteHandlerIfAuthenticated];
             }];
         }
     }
    

    Yet more cases must be distinguished within the invite handler, as matchForInvite:: is new in iOS6 as well and avoids yet another round through game center view controllers:

    -(void)setInviteHandlerIfAuthenticated
    {
        if ([GKLocalPlayer localPlayer].isAuthenticated) {
            [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
                if (acceptedInvite) {
                    if ([GKMatchmaker instancesRespondToSelector:@selector(matchForInvite:completionHandler:)]) {
                        [self showInfoAnimating:YES completion:NULL];
                        [[GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite
                                                      completionHandler:^(GKMatch *match, NSError *error) {
                                                          // ... handle invited match
                                                      }];
                    } else {
                        // alternative for iOS < 6
                        GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
                        mmvc.matchmakerDelegate = self;
                        // ... present mmvc appropriately
                        // ... handle invited match found in delegate method matchmakerViewController:didFindMatch:
                     }
                } else if (playersToInvite) {
                     // ... handle match initiated through game center
                }
            };
        }
    }
    

    Let me know if this helps.

提交回复
热议问题