我正在努力在我的游戏中集成基于回合的匹配,几天前我开始从GameKit API中得到奇怪的错误,说本地玩家没有经过身份验证,即使他是。
当我启动应用程序时,会调用authenticateHandler
,显示视图控制器,输入密码后,再次调用authenticaHandler
并且本地播放器似乎已经过身份验证。 isAuthenticated
会返回YES
。
但是一旦我开始使用任何GameKit API(如loadFriendsWithCompletionHandler:
),就会返回一个错误,说明该播放器尚未经过身份验证。
这是处理身份验证更改的代码。
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(UIViewController *viewController, NSError *error) {
if ([[GKLocalPlayer localPlayer] isAuthenticated]) {
// Player authenticated
} else {
// Player not authenticated
if (viewController != nil) {
// Present view controller
}
}
}];
这是我在调用任何GameKit方法时收到的错误消息。请注意,返回错误时-isAuthenticated
仍会返回YES。
找到匹配时出错:错误Domain = GKErrorDomain Code = 6"由于本地播放器尚未通过身份验证,因此无法完成请求的操作。" UserInfo = 0x14e9f950 {NSLocalizedDescription =由于本地播放器未经过身份验证,因此无法完成请求的操作。}
(lldb)print(BOOL)[[GKLocalPlayer localPlayer] isAuthenticated] (BOOL)3美元=是
我在Game Center沙盒中进行测试,几天前就开始了。以前,我根本没有遇到过这个问题 应用程序启动时,它只发生三次中的一次。我尝试删除应用程序,重新启动设备,清理构建文件夹以及我能想到的其他所有内容。
我是否遗漏了某些东西,或者是否有其他人遇到类似的问题?
答案 0 :(得分:1)
这个丰富的Apple documentation是一个值得关注的好地方。以下是我建议的两件事 -
如果您的设备有,则游戏中心无法完成身份验证 错误的日期。所以,请继续检查当前日期。
你可能已经这样做了。我相信你 - iOS模拟器>>重启 内容和设置
您认为使用-[GKLocalPlayer loadFriendsWithCompletionHandler:]
的方式可能有问题吗?您的身份验证功能没有任何问题,但如果适合您,我很乐意与您分享 -
-(void)authenticateLocalPlayer{
// Instantiate a GKLocalPlayer object to use for authenticating a player.
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController != nil) {
// If it's needed display the login view controller.
[self presentViewController:viewController animated:YES completion:nil];
}
else {
if ([GKLocalPlayer localPlayer].authenticated) {
// If the player is already authenticated then indicate that the Game Center features can be used.
_gameCenterEnabled = YES;
}
else {
_gameCenterEnabled = NO;
}
}
};
}
答案 1 :(得分:-1)
您为什么使用Game Kit Framework? 从iOS 7开始,您应该使用MultipeerConnectivity。
1) - authenticateWithCompletionHandler:在iOS 6.0中不推荐使用。 如果你在iOS 7上测试它可能根本不起作用。
2)下面通过MultipeerConnectivity创建连接的示例
客户端:
- (id)init {
self = [super init];
if (self) {
NSString *peerName = [NSString stringWithFormat:@"%@-%@", @"Client", [[UIDevice currentDevice] identifierForVendor].UUIDString];
self.myPeerID = [[MCPeerID alloc] initWithDisplayName:peerName];
self.servers = [NSMutableArray array];
self.session = [[MCSession alloc] initWithPeer:self.myPeerID securityIdentity:nil encryptionPreference:MCEncryptionNone];
self.session.delegate = self;
self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.myPeerID serviceType:@"Connect"];
self.browser.delegate = self;
[self.browser startBrowsingForPeers];
}
return self;
}
//-----
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {
NSLog(@"client: found a server: %@", peerID);
[self.browser invitePeer:peerID toSession:self.session withContext:nil timeout:10];
}
- (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID {
NSLog(@"client: lost server: %@", peerID);
}
#pragma mark - MCSessionDelegate
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
NSLog(@"client: status changed to %d for server: %@", state, peerID.displayName);
switch (state) {
case MCSessionStateNotConnected: {
}
break;
case MCSessionStateConnected: {
}
break;
default:
break;
}
}
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {
NSLog(@"client: received data (len = %lu) from server %@",(unsigned long)[data length], peerID.displayName);
NSDictionary *receiveDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
服务器:
- (id)init {
self = [super init];
if (self) {
NSString *peerName = [NSString stringWithFormat:@"%@-%@", @"Server", [[UIDevice currentDevice] identifierForVendor].UUIDString];
self.myPeerID = [[MCPeerID alloc] initWithDisplayName:peerName];
self.session = [[MCSession alloc] initWithPeer:self.myPeerID];
self.session.delegate = self;
self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.myPeerID
discoveryInfo:nil
serviceType:@"Connect"];
self.advertiser.delegate = self;
[self.advertiser startAdvertisingPeer];
}
return self;
}
#pragma mark - MCNearbyServiceAdvertiserDelegate
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler {
NSLog(@"server: did receive invitation from peer %@", peerID.displayName);
invitationHandler(YES, self.session);
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didNotStartAdvertisingPeer:(NSError *)error {
NSLog(@"server: error %@", error);
}
#pragma mark - MCSessionDelegate
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
NSLog(@"server: status changed to %ld for client: %@", state, peerID.displayName);
switch (state) {
case MCSessionStateConnected: {
NSMutableDictionary *sendDict = [NSMutableDictionary dictionary];
NSError *error = nil;
[self.session sendData:[NSKeyedArchiver archivedDataWithRootObject:sendDict]
toPeers:@[peerID]
withMode:MCSessionSendDataReliable
error:&error];
}
break;
case MCSessionStateNotConnected:
break;
default:
break;
}
NSLog(@"connectedPeers %@", self.session.connectedPeers);
}
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {
NSLog(@"server: received data (len = %lu) from client %@", (unsigned long)[data length], peerID.displayName);
NSDictionary *dictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID {
}
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress {
}
- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error {
}
- (void)session:(MCSession*)session didReceiveCertificate:(NSArray*)certificate fromPeer:(MCPeerID*)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler {
certificateHandler(YES);
}