App ID & Game Center - II (代码部分)

Game Center 需要 target iOS在 4.1及以上


- (BOOL)isGameCenterAvailable
{
// Check for presence of GKLocalPlayer API
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
// Check if the device is 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验证

[[GKLocalPlayer localPlayer]
    authenticateWithCompletionHandler:^(NSError *error) {
// Do something if necessary...
}];
a block of code that should be executed after the user successfully logs in or when there is an error. 


关注用户的认证状态通知

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil];
在用户第一次登陆时,这个 “authenticationChanged" 也会被调用。

所以如果想做一些用户登录成功后的处理,这里也可以做。


到project中,Build Phase 一tab, add Libraries >>  GameKit.framework

改为Optional。



创建NSObject的子类: GCHelper.h

#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
@interface GCHelper : NSObject {
BOOL gameCenterAvailable;
BOOL userAuthenticated;
}
+ (GCHelper *) sharedInstance;
- (void)authenticationChanged;
- (void)authenticateLocalUser;
@end

GCHelper.m

- (BOOL)isGameCenterAvailable {
// check for presence of GKLocalPlayer API
Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
// check if the device is 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);
}

#pragma mark Internal functions
- (void)authenticationChanged {
dispatch_async(dispatch_get_main_queue(), ^(void)
    {
if ([GKLocalPlayer localPlayer].isAuthenticated && 
           !userAuthenticated) {
NSLog(@"Authentication changed: player authenticated.");
           userAuthenticated = TRUE;
       } else if (![GKLocalPlayer localPlayer].isAuthenticated && 
                  userAuthenticated) {
NSLog(@"Authentication changed: player not authenticated");
           userAuthenticated = FALSE;
       }
    });
}

最后,如果你的app由一个development provisioning profile 来build的话,它启动的GC就是sandbox环境。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值