2014-02-13 120 views
3

随后在与iOS实现游戏中心雷Wenderlich教程7游戏中心iOS版7不工作

http://www.raywenderlich.com/3276/game-center-tutorial-for-ios-how-to-make-a-simple-multiplayer-game-part-12

不过我没有收到一个提示登录,也不是出现在迎回旗帜,试图许多教程,似乎无法得到它与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已经过时?

+1

@ sami-把你的认证 –

+0

代码@VivekSehrawat我已经添加了我的代码 – Sami

+0

@ Sami-具有u看到GKTapper的苹果示例代码...? –

回答

1

#从苹果示例代码导入“GameCenterManager.h”。你的项目目标设定到iOS 6.0将代码放在viewDidLoad中它会正常工作

@property (nonatomic, retain) GameCenterManager *gameCenterManager; 

    if([GameCenterManager isGameCenterAvailable]) 
     { 
     self.gameCenterManager= [[GameCenterManager alloc] init]; 
      [self.gameCenterManager setDelegate: self]; 
     [self.gameCenterManager authenticateLocalUser]; 
     } 
+0

请使用下面的链接安装游戏中心与排行榜。 “https://developer.apple.com/library/ios/documentation/LanguagesUtilities/Conceptual/iTunesConnectGameCenter_Guide/Leaderboards/Leaderboards.html” – dheerendra

5

这里是我使用的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 

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 

就用

[[gamecentercontrol sharedInstance] authenticateLocalUser] 

在任何你想让它完成身份验证您的视图控制器文件。

0
///////just Update the method 

- (void) authenticateLocalPlayer 

{ 

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; 

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) 
{ 


viewController=[[UIViewController alloc] init]; 

     if (viewController != nil) 

     { 

      //showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app. 

      [self showAuthenticationDialogWhenReasonable: viewController]; 

     } 

     else if (localPlayer.isAuthenticated) 

     { 

      //authenticatedPlayer: is an example method name. Create your own method that is called after the local player is authenticated. 

      [self authenticatedPlayer: localPlayer]; 

     } 

     else 

     { 

      [self disableGameCenter]; 

     } 

    }; 

} 
+0

欢迎来到Stack Overflow!虽然这段代码片段可能解决这个问题,但[包括解释](http://meta.stackexchange.com/questions/114762/explaining-entirely- code-based-answers)确实有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – NathanOliver