一、Game Center功能介绍
iOS中的 Game Center 是Apple提供的游戏中心服务,它具有以下核心功能:
- 玩家账号系统 - 提供玩家帐号系统,可以在不同游戏中使用统一的Game Center账号。
- 成就与排行榜 - 可以设置游戏内的成就系统,以及查看不同游戏的排行榜。
- 多人对战 - 支持通过WiFi或蓝牙进行的多人实时对战游戏。
- 社交功能 - 玩家可以在Game Center看到好友的游戏动态,还可以添加好友等。
- 云存储 - 提供基于iCloud的云存储功能,可以在不同设备上同步游戏进度。
- 登陆认证 - 提供了统一的Game Center登陆认证系统。
总之,Game Center为iOS游戏提供了丰富的社交、竞技功能,是游戏开发者可以直接集成使用的强大服务。它极大地丰富了iOS游戏的功能和体验。
二、iOS14升级
iOS14起,GameCenter升级了一些重要的代码,以积分榜为例:
旧代码,objective-c
#import "AppBasePageGame+GameCenter.h"
#import <GameKit/GameKit.h>
@implementation AppBasePageGame(GameCenter)
#pragma mark - GameCenter
//登录GameCenter:
-(void)goAuthenticate
{
void (^authenticateHandler)(UIViewController *viewController, NSError *error) = ^(UIViewController *viewController, NSError *error) {
if (viewController) {
// 需要展示登录界面
[self presentViewController:viewController animated:YES completion:nil];
} else if (error) {
// 验证错误
NSLog(@"Authentication failed: %@", error.localizedDescription);
} else {
// 验证成功
NSLog(@"Authentication succeeded");
}
};
[[GKLocalPlayer localPlayer] setAuthenticateHandler:authenticateHandler];
}
//显示排行榜:
-(void)goShowTop
{
if ([GKLocalPlayer localPlayer].isAuthenticated == NO) {
NSLog(@"没有授权,无法获取展示中心");
return;
}
GKGameCenterViewController *GCVC = [GKGameCenterViewController new];
//跳转指定的排行榜中
[GCVC setLeaderboardIdentifier:CONFIG_GAMECENTER_LEADERBOARD];
//跳转到那个时间段
NSString *type = @"all";
if ([type isEqualToString:@"today"]) {
[GCVC setLeaderboardTimeScope:GKLeaderboardTimeScopeToday];
}else if([type isEqualToString:@"week"]){
[GCVC setLeaderboardTimeScope:GKLeaderboardTimeScopeWeek];
}else if ([type isEqualToString:@"all"]){
[GCVC setLeaderboardTimeScope:GKLeaderboardTimeScopeAllTime];
}
GCVC.gameCenterDelegate = self;
[self presentViewController:GCVC animated:YES completion:nil];
}
//上传分数:
-(void)goSubmitScore
{
if ([GKLocalPlayer localPlayer].isAuthenticated == NO) {
NSLog(@"没有授权,无法获取展示中心");
return;
}
NSInteger userStar = [FrameUser getStar]*100;
GKScore *scoreBoard = [[GKScore alloc] initWithLeaderboardIdentifier:CONFIG_GAMECENTER_LEADERBOARD];
scoreBoard.value = userStar;
[GKScore reportScores:@[scoreBoard] withCompletionHandler:^(NSError *error) {
if (error) {
// handle error
}
}];
}
//实现代理:
//接受关闭回调,非常重要
#pragma mark - GKGameCenterControllerDelegate
- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController{
[gameCenterViewController dismissViewControllerAnimated:YES completion:nil];
}
@end
iOS14后的代码,swift
import UIKit
import SpriteKit
import GameplayKit
import AVFoundation
import GoogleMobileAds
import GameKit
import SwiftUI
class GameViewController_GameCenter: GameViewController, GKGameCenterControllerDelegate {
public var thisLeaderboardID="com.gpwzw.appChineseWordCross2.Score"
override func viewDidLoad() {
super.viewDidLoad()
self.goGameCenterAuthenticate()
}
//登录GameCenter:
func goGameCenterAuthenticate(){
GKLocalPlayer.local.authenticateHandler = { gcAuthVC, error in
if GKLocalPlayer.local.isAuthenticated {
print("Authenticated to Game Center!")
} else if let vc = gcAuthVC {
self.present(vc, animated: true)
}
else {
print("Error authentication to GameCenter: " +
"\(error?.localizedDescription ?? "none")")
}
}
}
//显示排行榜:
func showGameCenterLeaderboard(){
if GKLocalPlayer.local.isAuthenticated {
let gameCenterViewLeaderBoard = GKGameCenterViewController(leaderboardID: thisLeaderboardID, playerScope: GKLeaderboard.PlayerScope.global, timeScope: GKLeaderboard.TimeScope.today)
gameCenterViewLeaderBoard.gameCenterDelegate = self;
self.present(gameCenterViewLeaderBoard,animated: true);
}
else{
//self.goGameCenterAuthenticate();
}
}
//上传分数:
func postGameCenterLeaderboardAsync(_ score: Int) async{
if GKLocalPlayer.local.isAuthenticated {
Task{
try await GKLeaderboard.submitScore(
score,
context: 0,
player: GKLocalPlayer.local,
leaderboardIDs: [thisLeaderboardID]
)
}
print("Score submitted")
}
}
//上传分数:
func postGameCenterLeaderboardAll() async{
let intLastStage = appDelegate.gameConfig.getConfigNumber(CONFIG_LAST_STAGE)
let intScore = intLastStage * 100
if GKLocalPlayer.local.isAuthenticated {
Task{
try await GKLeaderboard.submitScore(
intScore,
context: 0,
player: GKLocalPlayer.local,
leaderboardIDs: [thisLeaderboardID]
)
}
print("Score submitted")
}
}
//实现代理:
//接受关闭回调,非常重要
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
print("game center did finish")
gameCenterViewController.dismiss(animated: true)
}
}
三、2023年8月16日新规
苹果邮件原文
Starting August 16, 2023, new apps and app updates that offer Game Center features need to include the Game Center entitlement and have Game Center features configured in App Store Connect before you can submit them to the App Store. Existing apps on the App Store are not affected by this new requirement.
We noticed that although the apps listed below have Game Center features configured in App Store Connect, their latest binary delivery doesn’t include the Game Center entitlement. In your next app update, please update your binary to include the entitlement.
Name: 疯狂填字
......
How to add the entitlement
If you’re automatically signing your app with Xcode and haven’t enabled the Game Center capability, enabling Game Center automatically adds the entitlement to the entitlement plist. If Game Center is already enabled in Xcode, and the com.apple.developer.game-center entitlement isn’t in the entitlements plist, remove and re-enable the Game Center capability.
If you’re manually signing your app, make sure the capability is enabled in Certificates, Identifiers & Profiles, generate a new profile, and add the com.apple.developer.game-center entitlement directly to your entitlements plist.
Learn more about diagnosing issues with entitlements to verify the entitlement is included in your app. If your app isn’t taking advantage of Game Center, you should remove the capability in Xcode and App Store Connect.
If you have any questions, contact us.
Apple Developer Relations
这个简单,把以下内容加到 info 最后面即可
<key>com.apple.developer.game-center</key>
<true/>
标签:Center,entitlement,import,XCode,GameCenter,Game,2023,GKLocalPlayer
From: https://www.cnblogs.com/gpwzw/p/17605316.html