2013-01-02 46 views
0

我希望有人能夠幫助解決Objective-C問題,它涉及使用GameCenter一次在兩個設備之間發送和接收信息他們已經通過配對。Objective-C:無法在使用Game Center的兩個配對兩個設備之後發送/接收數據

我正在使用一本叫做Beginning iOS Game Center和Game Kit的教科書作爲我的嚮導,它正在通過一個示例程序,但我被卡在想從設備上播放遊戲的數據中。

我可以成功matchmake兩個設備和相應的視圖出現。我在我的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

我意識到我在用同樣的東西在線觀察其他人的問題後所做的一切。在找到一場比賽之後,我沒有設置我的代表,這是一個非常新人的錯誤!

相關問題