功能
步骤:
1. 在iTunes Connect中启用Game Center。
2. 配置程序的Bundle identifier
3. 导入Game Kit Framework
4. 导入头文件<GameKit/GameKit.h>
5. 检测当前设备是否支持Game Center
6. 对于不支持Game Center的设备,对Game Kit Framework的引用应改为‘弱引用’(weak link)。
7. 程序启动后,认证用户。
一、iTunes Connect的配置
iTunes Connect Developer Guide
https://itunesconnect.apple.com/docs/iTunesConnect_DeveloperGuide.pdf/二、配置程序的Bundle identifier
com.myCompany.myCoolGame 与 iTunes Connect中配置相同
三、添加FrameWork (weak-link)检测当前设备是否支持Game Center
Target->’Get info’ ->’Genaral’ Linkd libray -> 将Required改为Weak
BOOL isGameCenterAvailable()
{
// Check for presence of GKLocalPlayer API.
Class gcClass = (NSClassFromString(@”GKLocalPlayer”));
// The device must be running running iOS 4.1 or later.
NSString *reqSysVer = @”4.1″;
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer
options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
四、认证用户
用户必须具有账户才可以访问Game center。程序启动或在需要调用Game Center功能的地方,认证当前的使用账户。
- (void) authenticateLocalUser{
if([GKLocalPlayer localPlayer].authenticated == NO){
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error){
if(error == nil){
//认证成功
}else{
//失败
UIAlertView* alert= [[[UIAlertView alloc] initWithTitle: @”Game Center Account Required”message: [NSString stringWithFormat: @"Reason: %@", [error localizedDescription]]delegate: self cancelButtonTitle: @”Try Again…” otherButtonTitles: NULL] autorelease];[alert show];
}}];
}
}
五、排行榜功能
5.1 在iTunes Connect中配置排行榜
(得分格式、排行榜分类)等
5.2 将得分发送到Game Center
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler: ^(NSError *error){
if(error != nil){
//处理储物
}else{
//显示排行榜
}}];
} 5.3 显示排行榜(LeaderboardController)
- (void) showLeaderboard{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil) {
leaderboardController.leaderboardDelegate = self;
[self presentModalViewController: leaderboardController animated: YES];
}
}
在显示leaderboard之前,可以配置leaderboard view Controller的一些属性。(category 属性,配置显示哪中分类下的排行榜;timeScope属性,配置显示哪个时间段内的排行榜)
//响应用户关闭排行榜的事件:
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[self dismissModalViewControllerAnimated:YES];
}
5.4 获取排行榜数据,自定义排行榜显示的view
//获取所有用户
-(void) retrieveTopTenScores
{
GKLeaderboard *leaderboardRequest = [GKLeaderboard alloc] init];
if (leaderboardRequest != nil) {
leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime;
leaderboardRequest.range = NSMakeRange(1,10);
[leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
if (error != nil)
// handle the error.
if (scores != nil)
// process the score information.
}];
}//获取特定用户的排行榜数据
-(void) receiveMatchBestScores: (GKMatch*) match
{
GKLeaderboard *query = [[GKLeaderboard alloc] initWithPlayers: match.players];
if (query != nil){
[query loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {}
if (error != nil)
// handle the error.
if (scores != nil)
// process the score information.
}];
}
六、Cocos2d 中加入 Game Center
6.1 方式1 (直接获取数据、自定义排行榜的view)
6。2 0.99.5 sdk of cocos2d have a RootViewController
6.3 方式3 使用viewcontroller
创建一个viewcontroller 将其view属性添加到cocos2d的 glview
UIViewController *tempVC=[[UIViewController alloc] init];
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
[tempVC presentModalViewController:mmvc animated:YES];
//响应事件
-(void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController
{
[tempVC dismissModalViewControllerAnimated:YES];
[tempVC.view removeFromSuperview];
}
//退出时
-(void) onExit
{
[super onExit];[tempVC release];
}
- (void) showLeaderboard
{
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil){
leaderboardController.leaderboardDelegate = self;
[[[CCDirector sharedDirector] openGLView] addSubview:tempVC.view];
[tempVC presentModalViewController:leaderboardController animated: YES];
}
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[tempVC dismissModalViewControllerAnimated:YES];
[tempVC.view removeFromSuperview];
}
Categories:iphone/ipadTags:
获取iphone、ipad、ipod具体版本信息
/*Erica Sadun, http://ericasadun.comiPhone Developer’s Cookbook, 3.0 EditionBSD License, Use at your own risk*/// Thanks to Emanuele Vulcano, Kevin Ballard/Eridius, Ryandjohnson, Matt Brown, etc.#include <sys/socket.h> // Per msqr#include <sys/sysctl.h>#include <net/if.h>#include <net/if_dl.h>#import “UIDevice-Hardware.h”@implementation UIDevice (Hardware)/*Platforms
iFPGA -> ??iPhone1,1 -> iPhone 1GiPhone1,2 -> iPhone 3GiPhone2,1 -> iPhone 3GSiPhone3,1 -> iPhone 4/AT&TiPhone3,2 -> iPhone 4/Other Carrier?iPhone3,3 -> iPhone 4/Other Carrier?iPhone4,1 -> ??iPhone 5iPod1,1 -> iPod touch 1GiPod2,1 -> iPod touch 2GiPod2,2 -> ??iPod touch 2.5GiPod3,1 -> iPod touch 3GiPod4,1 -> iPod touch 4GiPod5,1 -> ??iPod touch 5G
iPad1,1 -> iPad 1G, WiFiiPad1,? -> iPad 1G, 3G <- needs 3G owner to testiPad2,1 -> iPad 2G (iProd 2,1)
AppleTV2,1 -> AppleTV 2i386, x86_64 -> iPhone Simulator*/#pragma mark sysctlbyname utils- (NSString *) getSysInfoByName:(char *)typeSpecifier{size_t size;sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);char *answer = malloc(size);sysctlbyname(typeSpecifier, answer, &size, NULL, 0);NSString *results = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];free(answer);return results;}- (NSString *) platform{return [self getSysInfoByName:"hw.machine"];}// Thanks, Atomicbird- (NSString *) hwmodel{return [self getSysInfoByName:"hw.model"];}#pragma mark sysctl utils- (NSUInteger) getSysInfo: (uint) typeSpecifier{size_t size = sizeof(int);int results;int mib[2] = {CTL_HW, typeSpecifier};sysctl(mib, 2, &results, &size, NULL, 0);return (NSUInteger) results;}- (NSUInteger) cpuFrequency{return [self getSysInfo:HW_CPU_FREQ];}- (NSUInteger) busFrequency{return [self getSysInfo:HW_BUS_FREQ];}- (NSUInteger) totalMemory{return [self getSysInfo:HW_PHYSMEM];}- (NSUInteger) userMemory{return [self getSysInfo:HW_USERMEM];}- (NSUInteger) maxSocketBufferSize{return [self getSysInfo:KIPC_MAXSOCKBUF];}#pragma mark file system — Thanks Joachim Bean!- (NSNumber *) totalDiskSpace{NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];return [fattributes objectForKey:NSFileSystemSize];}- (NSNumber *) freeDiskSpace{NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];return [fattributes objectForKey:NSFileSystemFreeSize];}#pragma mark platform type and name utils- (NSUInteger) platformType{NSString *platform = [self platform];// if ([platform isEqualToString:@"XX"]) return UIDeviceUnknown;if ([platform isEqualToString:@"iFPGA"]) return UIDeviceIFPGA;if ([platform isEqualToString:@"iPhone1,1"]) return UIDevice1GiPhone;if ([platform isEqualToString:@"iPhone1,2"]) return UIDevice3GiPhone;if ([platform hasPrefix:@"iPhone2"]) return UIDevice3GSiPhone;if ([platform hasPrefix:@"iPhone3"]) return UIDevice4iPhone;if ([platform hasPrefix:@"iPhone4"]) return UIDevice5iPhone;if ([platform isEqualToString:@"iPod1,1"]) return UIDevice1GiPod;if ([platform isEqualToString:@"iPod2,1"]) return UIDevice2GiPod;if ([platform isEqualToString:@"iPod3,1"]) return UIDevice3GiPod;if ([platform isEqualToString:@"iPod4,1"]) return UIDevice4GiPod;if ([platform isEqualToString:@"iPad1,1"]) return UIDevice1GiPad;if ([platform isEqualToString:@"iPad2,1"]) return UIDevice2GiPad;if ([platform isEqualToString:@"AppleTV2,1"]) return UIDeviceAppleTV2;/*MISSING A SOLUTION HERE TO DATE TO DIFFERENTIATE iPAD and iPAD 3G…. SORRY!*/if ([platform hasPrefix:@"iPhone"]) return UIDeviceUnknowniPhone;if ([platform hasPrefix:@"iPod"]) return UIDeviceUnknowniPod;if ([platform hasPrefix:@"iPad"]) return UIDeviceUnknowniPad;if ([platform hasSuffix:@"86"] || [platform isEqual:@"x86_64"]) // thanks Jordan Breeding{if ([[UIScreen mainScreen] bounds].size.width < 768)return UIDeviceiPhoneSimulatoriPhone;elsereturn UIDeviceiPhoneSimulatoriPad;return UIDeviceiPhoneSimulator;}return UIDeviceUnknown;}- (NSString *) platformString{switch ([self platformType]){case UIDevice1GiPhone: return IPHONE_1G_NAMESTRING;case UIDevice3GiPhone: return IPHONE_3G_NAMESTRING;case UIDevice3GSiPhone: return IPHONE_3GS_NAMESTRING;case UIDevice4iPhone: return IPHONE_4_NAMESTRING;case UIDevice5iPhone: return IPHONE_5_NAMESTRING;case UIDeviceUnknowniPhone: return IPHONE_UNKNOWN_NAMESTRING;case UIDevice1GiPod: return IPOD_1G_NAMESTRING;case UIDevice2GiPod: return IPOD_2G_NAMESTRING;case UIDevice3GiPod: return IPOD_3G_NAMESTRING;case UIDevice4GiPod: return IPOD_4G_NAMESTRING;case UIDeviceUnknowniPod: return IPOD_UNKNOWN_NAMESTRING;case UIDevice1GiPad : return IPAD_1G_NAMESTRING;case UIDevice2GiPad : return IPAD_2G_NAMESTRING;case UIDeviceAppleTV2 : return APPLETV_2G_NAMESTRING;case UIDeviceiPhoneSimulator: return IPHONE_SIMULATOR_NAMESTRING;case UIDeviceiPhoneSimulatoriPhone: return IPHONE_SIMULATOR_IPHONE_NAMESTRING;case UIDeviceiPhoneSimulatoriPad: return IPHONE_SIMULATOR_IPAD_NAMESTRING;case UIDeviceIFPGA: return IFPGA_NAMESTRING;default: return IPOD_FAMILY_UNKNOWN_DEVICE;}}#pragma mark MAC addy// Return the local MAC addy// Courtesy of FreeBSD hackers email list// Accidentally munged during previous update. Fixed thanks to mlamb.- (NSString *) macaddress{int mib[6];size_t len;char *buf;unsigned char *ptr;struct if_msghdr *ifm;struct sockaddr_dl *sdl;mib[0] = CTL_NET;mib[1] = AF_ROUTE;mib[2] = 0;mib[3] = AF_LINK;mib[4] = NET_RT_IFLIST;if ((mib[5] = if_nametoindex(“en0″)) == 0) {printf(“Error: if_nametoindex error/n”);return NULL;}if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {printf(“Error: sysctl, take 1/n”);return NULL;}if ((buf = malloc(len)) == NULL) {printf(“Could not allocate memory. error!/n”);return NULL;}if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {printf(“Error: sysctl, take 2″);return NULL;}ifm = (struct if_msghdr *)buf;sdl = (struct sockaddr_dl *)(ifm + 1);ptr = (unsigned char *)LLADDR(sdl);// NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr,*(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];free(buf);return [outstring uppercaseString];}- (NSString *) platformCode{switch ([self platformType]){case UIDevice1GiPhone: return @”M68″;case UIDevice3GiPhone: return @”N82″;case UIDevice3GSiPhone: return @”N88″;case UIDevice4iPhone: return @”N89″;case UIDevice5iPhone: return IPHONE_UNKNOWN_NAMESTRING;case UIDeviceUnknowniPhone: return IPHONE_UNKNOWN_NAMESTRING;case UIDevice1GiPod: return @”N45″;case UIDevice2GiPod: return @”N72″;case UIDevice3GiPod: return @”N18″;case UIDevice4GiPod: return @”N80″;case UIDeviceUnknowniPod: return IPOD_UNKNOWN_NAMESTRING;case UIDevice1GiPad: return @”K48″;case UIDevice2GiPad: return IPAD_UNKNOWN_NAMESTRING;case UIDeviceUnknowniPad: return IPAD_UNKNOWN_NAMESTRING;case UIDeviceAppleTV2: return @”K66″;case UIDeviceiPhoneSimulator: return IPHONE_SIMULATOR_NAMESTRING;default: return IPOD_FAMILY_UNKNOWN_DEVICE;}}// Illicit Bluetooth check — cannot be used in App Store/*Class btclass = NSClassFromString(@”GKBluetoothSupport”);if ([btclass respondsToSelector:@selector(bluetoothStatus)]){printf(“BTStatus %d/n”, ((int)[btclass performSelector:@selector(bluetoothStatus)] & 1) != 0);bluetooth = ((int)[btclass performSelector:@selector(bluetoothStatus)] & 1) != 0;printf(“Bluetooth %s enabled/n”, bluetooth ? “is” : “isn’t”);}*/@end
另外有一个关于GameCenter的联网游戏文章写的很好,提供的代码,我还测试过,联网非常好。下边是传送门:(译)如何使用GameCenter制作一个简单的多人游戏教程:第一部分
(译)如何使用GameCenter制作一个简单的多人游戏教程:第二部分
另外还有一个关于游戏中心的博客也写的非常不错。
http://www.himigame.com/iphone-cocos2d/535.html
下边这个可能更详细:
编写苹果游戏中心应用程序(翻译 1.1 GCD和块对象介绍)
编写苹果游戏中心应用程序(翻译 1.2 创建游戏中心账户)
编写苹果游戏中心应用程序(翻译 1.3 为iOS应用程序设置游戏中心)
编写苹果游戏中心应用程序(翻译 1.4 添加游戏工具包框架)
编写苹果游戏中心应用程序(翻译 1.5 在游戏中心验证本地玩家)
编写苹果游戏中心应用程序(翻译 1.6 获取本地玩家的信息)
编写苹果游戏中心应用程序(翻译 1.7 在游戏中心添加朋友)
编写苹果游戏中心应用程序(翻译 1.8 获取本地玩家的好友信息)
编写苹果游戏中心应用程序(翻译 1.9 在iTunes Connect中创建排行榜)
编写苹果游戏中心应用程序(翻译 1.10 向排行榜提交得分)
编写苹果游戏中心应用程序(翻译 1.11 编程获取排行榜信息)编写苹果游戏中心应用程序(翻译 1.12 向玩家显示排行榜)
编写苹果游戏中心应用程序(翻译 1.13 在iTunes Connect中创建成就)
编写苹果游戏中心应用程序(翻译 1.14 向游戏中心提交成就)
编写苹果游戏中心应用程序(翻译 1.15 编程获取成就信息)