0

I know, there are some threads about this, but I didn't really get what I excatly have to do for my class.

- (void)authenticateLocalUser {

if (!gameCenterAvailable) return;

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

How can I fix the problem, it says "authenticateWithCompletionHandler" is deprecated.

Can somebody help me out?

EDIT:

With the new code

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

The error is : Capturing "localPlayer" strongly in this block is likely to lead to a retain circle . Its in the if(localPlayer.isAuthenticated).

also, the Game Center is not popping up anymore. If i want to let it pop up again with this code:

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
} 
};

Same Error here.

Here as img:

enter image description here

  • As said by the doc: "Deprecation Statement Set the authenticateHandler property instead." – Larme Sep 25 '15 at 12:08
  • possible duplicate of [authenticateWithCompletionHandler: is deprecated: first deprecated in iOS 6.0](http://stackoverflow.com/questions/14423995/authenticatewithcompletionhandler-is-deprecated-first-deprecated-in-ios-6-0) – Larme Sep 25 '15 at 12:08
  • authenticateWithCompletionHandler is deprecated in iOS6, you have to use setAuthenticateHandler (setAuthenticateHandler is new in iOS 6 ) – Peppo Sep 25 '15 at 12:12
  • I edited my post , have a look pls –  Sep 25 '15 at 12:19
  • Try the [answer](http://stackoverflow.com/questions/14423995/authenticatewithcompletionhandler-is-deprecated-first-deprecated-in-ios-6-0) at the link provided previously. You are creating the localPlayer within the completion block which you should not be doing. Here is a quick primer on [objc blocks](http://rypress.com/tutorials/objective-c/blocks) – Tommie C. Sep 25 '15 at 12:23
  • I did exactly, but I get the error thats on the picture above –  Sep 25 '15 at 12:27
  • That's a new error, just looking for it: http://stackoverflow.com/questions/7205128/fix-warning-capturing-an-object-strongly-in-this-block-is-likely-to-lead-to-a – Larme Sep 25 '15 at 12:31
  • Make the variable __weak or __block. – Tommie C. Sep 25 '15 at 12:52
  • yeah the __weak variable worked, im trying to add the game center login form when the user is not already logged in but its not working swell, look my comment unter the answer of Peppo –  Sep 25 '15 at 12:54
  • 1
    Considering the effort spent to fix this it might be worthwhile to update what worked for you. The code that resolved your problem may be useful to others and I noted the answer was not updated. Consider adding the solution to this question (especially if it is different from the prior asked question). – Tommie C. Sep 25 '15 at 16:22
  • i posted what worked for me –  Sep 25 '15 at 16:29

2 Answers2

1

You replaced the method with this method:

-(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
    }
   };
}
Peppo
  • 1,043
  • 1
  • 11
  • 19
  • Or adapt this method to yours – Peppo Sep 25 '15 at 12:25
  • You can add this "if" for safety: (I edit my answer) – Peppo Sep 25 '15 at 12:28
  • I did, but the warning is still there –  Sep 25 '15 at 12:30
  • The issue is that the localPlayer object is keeping a strong reference to itself, use __weak GKLocalPlayer *blockLocalPlayer = localPlayer; – Peppo Sep 25 '15 at 12:34
  • That worked to me thanks! now , if the player is not logged in i want to show the login form like [self presentViewController:viewController]; but i get an error the selector presentViewController is not declared. –  Sep 25 '15 at 12:38
  • additional info: i made a class GKTurnBasedMatchHelper where i have the method in, its a subclass of NSObject –  Sep 25 '15 at 12:41
  • Do you know what I mean ? :( –  Sep 25 '15 at 12:49
  • Yes.....emmm.....I think that you can use : [self showAuthenticationDialogWhenReasonable: viewController]; – Peppo Sep 25 '15 at 12:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90621/discussion-between-robin-and-peppo). –  Sep 25 '15 at 12:54
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
    }
};

}

this worked for me