接入GameCenter登录
target - Signing & Capabilities - + Capability - 添加 Game Center
1 2 3
| if([self isGameCenterAvailable]){ [self authenticateLocalUser]; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| //是否支持GameCenter - (BOOL) isGameCenterAvailable { Class gcClass = (NSClassFromString(@"GKLocalPlayer")); NSString *reqSysVer = @"4.1"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending); return (gcClass && osVersionSupported); }
//身份验证 - (void)authenticateLocalUser{ GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){ if (viewController != nil) { [m_viewController presentViewController:viewController animated:YES completion:nil]; } else{ if ([GKLocalPlayer localPlayer].authenticated) { // Get the default leaderboard identifier. [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) { if (error != nil) { NSLog(@"%@", [error localizedDescription]); } else{ } }]; } else{ } } }; }
|