Sam*_*ami 3 leaderboard ios game-center ios7
遵循Ray Wenderlich关于使用iOS 7实现Game Center的教程
但是,我没有得到登录的提示,也没有欢迎回来横幅出现,尝试了很多教程,似乎无法让它与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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
并在我的应用程序确实完成了我的启动方法
[[GCHelper sharedInstance] authenticateLocalUser];
Run Code Online (Sandbox Code Playgroud)
是因为我authenticateWithCompletionHandler被弃用了吗?
这是我用来显示使用iOS7登录游戏中心的代码
gamecentercontrol.h
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
@interface gamecentercontrol : NSObject {
BOOL gameCentreAvailable;
BOOL userAuthenticated;
}
@property (assign, readonly) BOOL gameCentreAvailable;
+ (gamecentercontrol *)sharedInstance;
-(void)authenticateLocalUser;
@end
Run Code Online (Sandbox Code Playgroud)
gamecentercontrol.m
#import "gamecentercontrol.h"
@interface gamecentercontrol () <GKGameCenterControllerDelegate> {
BOOL _gameCenterFeaturesEnabled;
}
@end
@implementation gamecentercontrol
@synthesize gameCentreAvailable;
static gamecentercontrol *sharedControl = nil;
+ (gamecentercontrol *) sharedInstance {
if (!sharedControl) {
sharedControl = [[gamecentercontrol alloc]init];
}
return sharedControl;
}
-(BOOL)isGameCentreAvailable {
// 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])) {
gameCentreAvailable = [self isGameCentreAvailable];
if (gameCentreAvailable) {
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. User Authenticated");
userAuthenticated = TRUE;
}
else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
NSLog(@"Authentication Changed. User Not Authenticated");
userAuthenticated = FALSE;
}
}
-(void) authenticateLocalUser {
if ([GKLocalPlayer localPlayer].authenticated == NO) {
GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *gcvc,NSError *error) {
if(gcvc) {
[self presentViewController:gcvc];
}
else {
_gameCenterFeaturesEnabled = NO;
}
};
}
else if ([GKLocalPlayer localPlayer].authenticated == YES){
_gameCenterFeaturesEnabled = YES;
}
}
-(UIViewController*) getRootViewController {
return [UIApplication sharedApplication].keyWindow.rootViewController;
}
-(void)presentViewController:(UIViewController*)gcvc {
UIViewController* rootVC = [self getRootViewController];
[rootVC presentViewController:gcvc animated:YES completion:nil];
}
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController {
}
@end
Run Code Online (Sandbox Code Playgroud)
然后就用吧
[[gamecentercontrol sharedInstance] authenticateLocalUser]
Run Code Online (Sandbox Code Playgroud)
在您的视图控制器文件中,您希望它在哪里完成您的身份验证.
| 归档时间: |
|
| 查看次数: |
2062 次 |
| 最近记录: |