首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏中心iOS 7不工作

游戏中心iOS 7不工作
EN

Stack Overflow用户
提问于 2014-02-13 04:02:40
回答 3查看 2.1K关注 0票数 3

遵循Ray教程,使用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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

在我的应用程序中,我已经完成了启动方法

代码语言:javascript
复制
[[GCHelper sharedInstance] authenticateLocalUser];

是因为我不赞成authenticateWithCompletionHandler吗?

EN

回答 3

Stack Overflow用户

发布于 2014-02-27 06:29:41

下面是我用iOS7显示游戏中心登录的代码

gamecentercontrol.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

那就用

代码语言:javascript
复制
[[gamecentercontrol sharedInstance] authenticateLocalUser]

在您的视图控制器文件中,您希望它在哪里完成您的身份验证。

票数 5
EN

Stack Overflow用户

发布于 2014-02-14 06:36:12

#从苹果示例代码中导入"GameCenterManager.h“。将您的项目目标设置为iOS 6.0,将代码放到视图加载中,这样会很好

代码语言:javascript
复制
   @property (nonatomic, retain) GameCenterManager *gameCenterManager;

    if([GameCenterManager isGameCenterAvailable])
      {
        self.gameCenterManager= [[GameCenterManager alloc] init];
            [self.gameCenterManager setDelegate: self];
        [self.gameCenterManager authenticateLocalUser];
        }
票数 1
EN

Stack Overflow用户

发布于 2015-11-16 14:23:01

代码语言:javascript
复制
///////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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21744981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档