首先要在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);
}
}
}];
}