My view controller no longer gets deallocated after adding the following:
@property (strong, nonatomic) GKLocalPlayer *player;
(in viewDidLoad)
self.player = nil;
[self authenticatePlayer];
- (void)authenticatePlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
__unsafe_unretained typeof(*localPlayer) *blockLocalPlayer = localPlayer;
localPlayer.authenticateHandler =
^(UIViewController *authenticateViewController, NSError *error)
{
if (authenticateViewController != nil)
{
[self presentViewController:authenticateViewController animated:YES
completion:nil];
}
else if (blockLocalPlayer.isAuthenticated)
{
self.player = blockLocalPlayer;
[self openGame];
}
else
{
// Disable Game Center
self.player = nil;
[self openGame];
}
};
}
- (void)setPlayer:(GKLocalPlayer *)player
{
_player = player;
NSString *playerName;
if (_player)
{
playerName = _player.alias;
}
else
{
playerName = @"Anonymous Player";
}
NSLog(@"%@", [NSString stringWithFormat:@"Welcome %@", playerName]);
}
The problem occurs whether or not the user connects to game center. There must be something in the code that is causing the view controller to remain in memory after it gets dismissed. If I comment these lines out:
self.player = nil;
[self authenticatePlayer];
the view controller will properly get deallocated when dismissed.
EDIT: My hunch was correct. From Apple docs:
Game Kit maintains a strong reference to your completion handler even after successfully authenticating a local player. If your game moves into the background, Game Kit automatically authenticates the player again whenever your game moves back to the foreground. Game Kit calls your same completion handler each time it authenticates the local player. Be mindful that in block programming, any Objective-C object referenced inside a block is also strongly referenced by the block until the block is released. Because Game Kit maintains a strong reference to your completion handler until your game terminates, any objects referenced from within your authentication handler are also held indefinitely.
This is a problem for me though. I'm using Cocos2d and it has problems resetting its view unless the view controller is completely deallocated and created fresh.
Is there any way to get Game Kit to let go of my view controller?