在程序中添加Game Center功能
来源:互联网 发布:京津冀 知乎 编辑:程序博客网 时间:2024/05/16 01:57
步骤:1.在iTunes Connect中启用Game Center。2.配置程序的Bundle identifier3.导入Game Kit Framework4.导入头文件<GameKit/GameKit.h>5.检测当前设备是否支持Game Center6.对于不支持Game Center的设备,对Game Kit Framework的引用应改为‘弱引用’(weak link)。7.程序启动后,认证用户。 四、认证用户用户必须具有账户才可以访问Game center。程序启动或在需要调用Game Center功能的地方,认证当前的使用账户。 五、排行榜功能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]; 在显示leaderboard之前,可以配置leaderboard view Controller的一些属性。(category 属性,配置显示哪中分类下的排行榜;timeScope属性,配置显示哪个时间段内的排行榜) //响应用户关闭排行榜的事件: 5.4 获取排行榜数据,自定义排行榜显示的view 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];} { [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]; } } { [tempVC dismissModalViewControllerAnimated:YES]; [tempVC.view removeFromSuperview]; } /* Erica Sadun, http://ericasadun.com iPhone Developer’s Cookbook, 3.0 Edition BSD 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 1G iPhone1,2 ->iPhone 3G iPhone2,1 ->iPhone 3GS iPhone3,1 ->iPhone 4/AT&T iPhone3,2 ->iPhone 4/Other Carrier? iPhone3,3 ->iPhone 4/Other Carrier? iPhone4,1 ->??iPhone 5 iPod1,1 -> iPod touch 1G iPod2,1 -> iPod touch 2G iPod2,2 -> ??iPod touch 2.5G iPod3,1 -> iPod touch 3G iPod4,1 -> iPod touch 4G iPod5,1 -> ??iPod touch 5G iPad1,1 -> iPad 1G, WiFi iPad1,? -> iPad 1G, 3G <- needs 3G owner to test iPad2,1 -> iPad 2G (iProd 2,1) AppleTV2,1 -> AppleTV 2 i386, 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; else return 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在程序中添加Game Center功能
一、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:reqSysVeroptions:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);}
- (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]; } } ];}}
if (leaderboardController != nil) {leaderboardController.leaderboardDelegate = self; [self presentModalViewController: leaderboardController animated: YES];}}
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {[self dismissModalViewControllerAnimated:YES]; }
//获取所有用户- (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
//退出时-(void) onExit
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController获取iphone、ipad、ipod具体版本信息
- 在程序中添加Game Center功能
- 在程序中添加Game Center功能(转)
- 在桌面程序中添加GPS功能
- 在c++程序中添加计时功能
- 在代码中使用Game Center Learderboard的操作
- 基于ssm框架的个人博客(4)--点击菜单中的功能,在center中添加tab
- Cocos2d-X 添加 Game Center 的方法
- Cocos2d-X 添加 Game Center 的方法
- 在MFC程序中添加全屏显示功能
- 在MFC程序中添加全屏显示功能
- Game Center
- 在Menu中添加功能
- android 中添加 Google Play Game Services 功能 1
- 在程序中添加背景音乐!...
- 在程序中添加广告
- 在程序中添加广告
- 用VB在程序中添加切换中文输入法的功能
- vShpere Center添加镜像到数据存储中
- object is not an instance of declaring class 反射异常
- android网络数据开关
- OpenGL敌人跟踪主角的算法
- (译)如何使用GameCenter制作一个简单的多人游戏教程:第一部分
- 郁闷
- 在程序中添加Game Center功能
- 这
- TCP 协议 头结构
- (译)如何使用GameCenter制作一个简单的多人游戏教程:第二部分
- tftp 协议分析
- Python常用模块资料
- 我让鬼佬吃醋了:WP7截图工具诞生攻坚实录(三)
- 内存管理(1)
- Flash Builder 4.5 导入一个新项目,提示“flex unable to open xxxxxxxxx.swc”