authPlayerWithCompletionHandler已弃用,所以如何使用authenticateHandler

问题描述:

标题中的问题非常多,authPlayerWithCompletionHandler已弃用,所以如何使用authenticateHandler?authPlayerWithCompletionHandler已弃用,所以如何使用authenticateHandler

+0

阅读API差异。 –

setAuthenticateHandler是新在IOS 6,authenticateWithCompletionHandler仍然必须在IOS 5和下面使用的。

另外,为presentViewController提供一个完成处理程序:animated:completion:并不是必须的,因为完成处理程序在显示游戏中心视图之后调用,而不是在完成时调用。

这里是我的解决方案:

- 在iOS 4.3,iOS的5.1,iOS的测试,只有6.0模拟器 - 而不是实际的设备。

注意 - 这里假定您已经检查过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

只需将Deployment Target设置为例如5.0或任何你的目标 - 选择项目(项目导航器中的首行)>在目标>摘要>部署目标中选择你的应用程序。 –

这就是我想出的 - 它似乎工作。如果你认为我错过了任何东西,请随时编辑。

-(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 
     } 
    })]; 
} 

我使用这个代码适用于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 
{ 

}