我希望有人能够帮助解决Objective-C问题,这个问题与我们通过配对制作后使用GameCenter在两台设备之间发送和接收信息有关。
我正在使用一本名为Beginning iOS Game Center和Game Kit的教科书作为我的向导,它正在通过一个示例程序,但我仍然希望从玩游戏的设备接收数据。
我可以成功匹配两个设备并显示相应的视图。我在GameCenterManager.m文件中有两个函数来发送信息 - 一个是以下内容:
- (void)sendStringToAllPeers:(NSString *)dataString reliable:(BOOL)reliable
{
NSLog(@"Send String To All Peers");
NSLog(@"Data String: %@", dataString);
NSLog(@"match or session %@", self.matchOrSession);
if (self.matchOrSession == nil)
{
NSLog(@"GC Manager matchorsession ivar was not set - this needs to be set with the GKMatch or GKSession before sending or receiving data");
return;
}
NSData *dataToSend = [dataString dataUsingEncoding:NSUTF8StringEncoding];
GKSendDataMode mode;
if (reliable)
{
mode = GKSendDataReliable;
}
else{
mode = GKSendDataUnreliable;
}
NSError *error = nil;
if ([self.matchOrSession isKindOfClass:[GKSession class]])
{
NSLog(@"Match or session 1");
NSLog(@"Data to send: %@", dataToSend);
[self.matchOrSession sendDataToAllPeers:dataToSend withDataMode:mode error:&error];
}
else if ([self.matchOrSession isKindOfClass:[GKMatch class]])
{
NSLog(@"Match or session 2");
NSLog(@"Data to send: %@", dataToSend);
[self.matchOrSession sendDataToAllPlayers:dataToSend withDataMode:mode error:&error];
}
else
{
NSLog(@"GC Manager matchOrSession was not a GKMatch or a GK Session, we are unable to send data");
}
if (error != nil)
{
NSLog(@"An error occurred while sending data %@", [error localizedDescription]);
}
}
我从racetohundredViewController.m文件中的函数调用此函数:
- (void)generateAndSendHostNumber;
{
NSLog(@"Generate and send host number");
randomHostNumber = arc4random();
NSString *randomNumberString = [NSString stringWithFormat: @"$Host:%f", randomHostNumber];
NSLog(@"the random number string is: %@", randomNumberString);
[self.gcManager sendStringToAllPeers:randomNumberString reliable: YES];
}
我成功获得了以下NSLog输出:
2013-01-02 22:27:43.519 First to 50[1376:907] Send String To All Peers
2013-01-02 22:27:43.520 First to 50[1376:907] Data String: $Host:2087825492.000000
2013-01-02 22:27:43.521 First to 50[1376:907] match or session <GKMatch 0x200853d0 expected count: 0 seqnum: 2
G:1656671636:connected
reinvitedPlayers:(
)>
2013-01-02 22:27:43.522 First to 50[1376:907] Match or session 2
2013-01-02 22:27:43.523 First to 50[1376:907] Data to send: <24486f73 743a3230 38373832 35343932 2e303030 303030>
所以我可以看到'数据发送'输出很棒。
但是我现在有了命令
[self.matchOrSession sendDataToAllPeers:dataToSend withDataMode:mode error:&error];
这似乎根本不带我到任何地方。我在GameCenterManager.m中有以下功能:
- (void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
NSLog(@"*****Receive Data In Session");
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:dataString, peer, session, nil] forKeys:[NSArray arrayWithObjects: @"data", @"peer", @"session", nil]];
[self callDelegateOnMainThread: @selector(receivedData:) withArg: dataDictionary error: nil];
}
但是我没有看到NSLog的输出。同样,我在我的racetohundredViewController.m文件中有一个函数
- (void)receivedData:(NSDictionary *)dataDictionary
{
NSLog(@"------Received Data");
}
也没有被调用;大概是因为之前的功能无法调用它。
我一直试图解决为什么现在这种情况不起作用一段时间没有任何效果。任何人都可以指出我哪里错了吗?我希望我已经提交了所有相关代码,但如果您有任何疑问,请询问。
提前感谢所有人。
答案 0 :(得分:0)
我意识到我在网上看到其他人的同样问题后做了什么。在找到比赛后,我没有设置我的代表,这是一个非常新的错误!