-
首先要在Apple Developer开发平台配置项目,增加排行榜和成就各一项,然后删除了就行,用来激活GameCenter功能。
-
使用GameKit系统库,在项目的Capabilities中打开Game Center功能选项
-
编写代码
此中遇到一个很傻的问题,获取用户昵称,开始一直获取的displayName
中的值,返回为“Me”,嗯...不符合要求,查资料,找问题。。。。最后发现,应该用alias
总结:要多打印log,不要盲目的去找问题,更不要看词取义!!!
- (void)viewDidLoad {
[super viewDidLoad];
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer setAuthenticateHandler:^(UIViewController * _Nullable viewController, NSError * _Nullable error) {
if (viewController != nil) {
[self presentViewController:viewController animated:YES completion:nil];
}else {
GKLocalPlayer *player = [GKLocalPlayer localPlayer];
if (player.isAuthenticated) {
NSLog(@"id:%@ --- name:%@ --- alias:%@",player.playerID, player.displayName,player.alias);
}
}
}];
}