title: Objective-C: Unable to send/receive data after two matchmaking two devices using Game Center
body: <p>I was hoping someone would be able to help with an Objective-C problem I have relating to sending and receiving information between two devices using GameCenter once they have been through matchmaking. </p>
  
  <p>I am using a textbook called Beginning iOS Game Center and Game Kit as my guide, and it is going through an example program, but I am stuck on the part where I would like to receive data from a device playing the game. </p>
  
  <p>I can successfully matchmake two devices and the appropriate view to appear. I have two functions in my GameCenterManager.m file to send information - one being the following:</p>
  
  <pre><code>- (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:&amp;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:&amp;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]);
  
  }
  }
  </code></pre>
  
  <p>this function I call from a function in my racetohundredViewController.m file:</p>
  
  <pre><code>- (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];
  }
  </code></pre>
  
  <p>I successfully get the following NSLog output resulting from this: </p>
  
  <pre><code>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 &lt;GKMatch 0x200853d0 expected count: 0 seqnum: 2
  G:1656671636:connected
  reinvitedPlayers:(
  )&gt;
  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: &lt;24486f73 743a3230 38373832 35343932 2e303030 303030&gt;
  </code></pre>
  
  <p>So I can see the 'Data to send' output which is great. </p>
  
  <p>However I now have the command </p>
  
  <pre><code>[self.matchOrSession sendDataToAllPeers:dataToSend withDataMode:mode error:&amp;error];
  </code></pre>
  
  <p>which doesn't seem to take me anywhere at all. I have the following function in GameCenterManager.m:</p>
  
  <pre><code>- (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];
  }
  </code></pre>
  
  <p>But I am not seeing the NSLog output from this. Likewise, I have a function in my racetohundredViewController.m file </p>
  
  <pre><code>- (void)receivedData:(NSDictionary *)dataDictionary
  {
      NSLog(@"------Received Data");
  }
  </code></pre>
  
  <p>which also doesn't get called; presumably because the previous function isn't able to call it.</p>
  
  <p>I have been trying to work out why this doesn't work for a while now without any avail. Can anyone point out where I am going wrong? I hope I have put in all the relevant code but if you have any questions please ask. </p>
  
  <p>Thanks to all, in advance.</p>