不推荐使用的方法authenticateWithCompletionHandler

时间:2015-09-25 12:06:21

标签: ios objective-c

我知道,有一些关于此问题的线索,但我并没有真正得到我为我的班级所做的一切。

- (void)authenticateLocalUser {

if (!gameCenterAvailable) return;

NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
    [[GKLocalPlayer localPlayer]
     authenticateWithCompletionHandler:nil];
} else {
    NSLog(@"Already authenticated!");
}
}

如何解决问题,它说“authenticateWithCompletionHandler”已被弃用。

有人可以帮帮我吗?

编辑:

使用新代码

- (void)authenticateLocalUser {

if (!gameCenterAvailable) return;

NSLog(@"Authenticating local user...");

if ([GKLocalPlayer localPlayer].authenticated == NO) {

    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
        //[localPlayer authenticateWithCompletionHandler:^(NSError *error) { OLD CODE!
        if(localPlayer.isAuthenticated) {
            //do some stuff
        }else {
            // not logged in
        }
    })];
} else {
    NSLog(@"Already authenticated!");
}   
}

错误是:在此块中强烈捕获“localPlayer”可能会导致保留圈。它位于if(localPlayer.isAuthenticated)。

此外,游戏中心不再弹出。如果我想让它再次弹出这段代码:

localPlayer.authenticateHandler = ^(UIViewController *viewController,NSError *error) {
if (localPlayer.authenticated) { 
//already authenticated
} else if(viewController) {
[self presentViewController:viewController];//present the login form
} else {
//problem with authentication,probably bc the user doesn't use Game    Center
} 
};

同样的错误。

这里是img:

enter image description here

2 个答案:

答案 0 :(得分:1)

您使用此方法替换了该方法:

-(void)authenticateLocalUser {


NSLog(@"Authenticating local user ...");
if(!gameCenterAvailable) {
    return;
}

    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
    NSLog(@"authenticateHandler");
    if (viewController != nil)
    {
        NSLog(@"viewController != nil");
    }
    else if (localPlayer.isAuthenticated)
    {
        NSLog(@"localPlayer already authenticated");
        //do some stuff
    }
    else
    {
        NSLog(@"local player not authenticated");
        // not logged in
    }
   };
}

答案 1 :(得分:0)

- (void)authenticateLocalUser {

if(!gameCenterAvailable) { return; }

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
__weak GKLocalPlayer *blockLocalPlayer = localPlayer;

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
    NSLog(@"authenticateHandler");
    if (viewController != nil)
    {
        NSLog(@"viewController != nil");
    }
    else if (blockLocalPlayer.isAuthenticated)
    {
        NSLog(@"localPlayer already authenticated");
        //do some stuff
    }
    else
    {
        NSLog(@"local player not authenticated");
        // not logged in
    }
};

}

这对我有用