【发布时间】:2014-03-11 18:50:11
【问题描述】:
按照 Ray Wenderlich 教程使用 iOS 7 实现游戏中心
但是我没有收到登录提示,也没有出现欢迎返回横幅,尝试了许多教程,似乎无法让它与 iOS 7 一起使用。
这是我的代码
GCHelper.h
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
@interface GCHelper : NSObject {
BOOL gameCenterAvailable;
BOOL userAuthenticated;
}
@property (assign, readonly) BOOL gameCenterAvailable;
+ (GCHelper *)sharedInstance;
- (void)authenticateLocalUser;
@end
GCHelper.m
#import "GCHelper.h"
@implementation GCHelper
@synthesize gameCenterAvailable;
#pragma mark Initialization
static GCHelper *sharedHelper = nil;
+ (GCHelper *) sharedInstance {
if (!sharedHelper) {
sharedHelper = [[GCHelper alloc] init];
}
return sharedHelper;
}
- (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);
}
- (id)init {
if ((self = [super init])) {
gameCenterAvailable = [self isGameCenterAvailable];
if (gameCenterAvailable) {
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
}
return self;
}
- (void)authenticationChanged {
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;
}
}
#pragma mark User functions
- (void)authenticateLocalUser {
if (!gameCenterAvailable) return;
NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
} else {
NSLog(@"Already authenticated!");
}
}
@end
并且在我的应用程序中确实完成了我的启动方法
[[GCHelper sharedInstance] authenticateLocalUser];
是因为我的authenticateWithCompletionHandler 被弃用了吗?
【问题讨论】:
-
@sami- 输入你的验证码
-
@VivekSehrawat 我已经添加了我的代码
-
@Sami- 你见过苹果 GKTapper 的示例代码吗...?
-
是的,我看过并尝试实现它,但没有运气。
-
@sami- 你试过我的答案吗
标签: ios ios7 game-center leaderboard