2012-09-13 4 views

답변

17

setAuthenticateHandler는 아이폰 OS 6, authenticateWithCompletionHandler 여전히 아래에서 iOS 5 및 사용해야합니다 새로운 기능입니다.

또한 presentViewController : animated : completion :에 대한 완료 핸들러를 제공하는 것은 실제로 완료되지 않았지만 게임 센터보기가 표시된 후에 완료 핸들러가 호출되기 때문에 실제로 필요하지 않습니다. 하지 실제 장치에서 - - 6.0 시뮬레이터에서만 아이폰 OS 4.3, 아이폰 OS 5.1, iOS에서 테스트

참고 :

여기 내 솔루션입니다.

참고 - 이것은 GameCenter API를 사용할 수 있는지 확인한 것으로 간주합니다.

- (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

잘 작동합니다! 그래도 사용 중지 된 메소드에 대한 경고 플래그를 제거 할 수 있습니까? – msgambel

+0

배포 목표를 예 : 5.0 또는 대상이 무엇이든간에 - 프로젝트 (프로젝트 탐색기의 맨 위 줄)를 선택하고 대상> 요약> 배포 대상에서 앱을 선택합니다. –

3

이것은 내가 생각해 낸 것입니다. 작동하는 것 같습니다. 내가 무엇이든 놓쳤다 고 생각하면 편집 해주십시오.

-(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

iOS 6 이상에서는이 코드를 사용하고 있습니다. 컴파일러 오류가 없으며 제대로 작동하는 것 같습니다.

#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 
{ 

} 
관련 문제