关闭

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

标签: authenticationsandboxbuildinterfaceiosobject
2187人阅读 评论(0) 收藏 举报

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环境。







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43674次
    • 积分:709
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:5篇
    • 译文:19篇
    • 评论:0条
    文章存档