2012-09-13 3 views

Antwort

17

setAuthenticateHandler ist neu in iOS 6, authenticateWithCompletionHandler noch in iOS 5 und unten verwendet werden muss.

Auch ein Abschluss-Handler für presentViewController: animated: completion: ist nicht wirklich notwendig, da dieser Completion-Handler aufgerufen wird, nachdem die Game Center-Ansicht angezeigt wird, nicht wenn sie abgeschlossen ist.

Hier ist meine Lösung:

HINWEIS - getestet auf iOS 4.3, iOS 5.1, iOS 6.0 Simulatoren - nur nicht auf einem tatsächlichen Gerät.

HINWEIS - Dies setzt voraus, dass Sie überprüft haben, dass die GameCenter-API verfügbar ist.

- (void)checkLocalPlayer 
{ 
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; 

    if (localPlayer.isAuthenticated) 
    { 
     /* Perform additional tasks for the authenticated player here */ 
    } 
    else 
    { 
     /* Perform additional tasks for the non-authenticated player here */ 
    } 
} 

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] \ 
compare:v options:NSNumericSearch] == NSOrderedAscending) 

- (void)authenticateLocalPlayer 
{ 
     GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; 

     if (SYSTEM_VERSION_LESS_THAN(@"6.0")) 
     { 
      // ios 5.x and below 
      [localPlayer authenticateWithCompletionHandler:^(NSError *error) 
      { 
       [self checkLocalPlayer]; 
      }]; 
     } 
     else 
     { 
      // ios 6.0 and above 
      [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) { 
       if (!error && viewcontroller) 
       { 
        [[AppDelegate sharedDelegate].viewController 
        presentViewController:viewcontroller animated:YES completion:nil]; 
       } 
       else 
       { 
        [self checkLocalPlayer]; 
       } 
      })]; 
     } 
    } 
} 
+0

Funktioniert gut! Wie werden Sie jedoch das Warnflag für die veraltete Methode loswerden? – msgambel

+0

Setzen Sie das Deployment-Ziel einfach auf z. 5.0 oder was auch immer Ihr Ziel ist - wählen Sie das Projekt (oberste Zeile im Projektnavigator)> wählen Sie Ihre App unter Ziele> Zusammenfassung> Einsatzziel. –

3

Das ist, was ich gefunden habe - es scheint zu funktionieren. Fühlen Sie sich frei zu bearbeiten, wenn Sie denken, dass ich etwas verpasst habe.

-(void)authenticatePlayer { 
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; 
    [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) { 
     if (!error) { 
      [self presentViewController:viewcontroller animated:YES completion:^{ 
       if (localPlayer.isAuthenticated) 
       { 
        // your code if authenticated 
       } 
       else { 
        // your code if not authenticated 
       } 
      }]; 
     } 
     else { 
      // error handling code here 
     } 
    })]; 
} 
5

Ich verwende diesen Code für iOS 6 und höher. Es gibt keine Compilerfehler und es scheint zu funktionieren.

#pragma 
#pragma mark - Player Authentication 
-(void)autheticatePlayer 
{ 
    __weak typeof(self) weakSelf = self; // removes retain cycle error 

    _localPlayer = [GKLocalPlayer localPlayer]; // localPlayer is the public GKLocalPlayer 
    __weak GKLocalPlayer *weakPlayer = _localPlayer; // removes retain cycle error 

    weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) 
    { 
     if (viewController != nil) 
     { 
      [weakSelf showAuthenticationDialogWhenReasonable:viewController]; 
      } 
      else if (weakPlayer.isAuthenticated) 
      { 
       [weakSelf authenticatedPlayer:weakPlayer]; 
      } 
      else 
      { 
       [weakSelf disableGameCenter]; 
      } 
      }; 
} 

-(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller 
{ 
    [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil]; 
} 

-(void)authenticatedPlayer:(GKLocalPlayer *)player 
{ 
    player = _localPlayer; 
} 

-(void)disableGameCenter 
{ 

}