1

Hello I am sending data using gksession.code is given below.Here send data works fine and gives message that did data send.But i am not able to receive any data on other device.When i debug the code it is not calling and going inside the method receive data.Does anyone have any idea about this?What's wrong here or how can i solve the problem?

- (void)viewDidLoad {
    [super viewDidLoad];
    gs=[manager getgkSession];
    //[cs retain]
    self.title = [manager displayNameForPeer:manager.currentConfPeerID];
    NSLog(@"name of mangager is %@",self.title);

}


-(IBAction) btnSend:(id) sender
{
    //---convert an NSString object to NSData---
    NSData* data;
    NSError *error=nil;
    NSString *str = [NSString stringWithString:txtMessage.text];
    data = [str dataUsingEncoding: NSASCIIStringEncoding];     
    if (gs) 

    {
        [self.gs sendDataToAllPeers:data 
                       withDataMode:GKSendDataReliable 
                              error:&error];    
            if (!error) 
        {
            NSLog(@"Did send data");
        } 
        else 
        {
            NSLog(@"Send data failed: %@", [error localizedDescription]);
        }   
    }        
}


    - (void) receiveData:(NSData *)data 
                fromPeer:(NSString *)peer 
               inSession:(GKSession *)session 
                 context:(void *)context {

        //---convert the NSData to NSString---
        NSString* str;
        str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];    
        NSLog(@"data received is %@",str);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data received" 
                                                        message:str 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        NSLog(@"receive data");
        [alert show];
        [alert release];    
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
nehal
  • 622
  • 1
  • 13
  • 26

2 Answers2

2

Did you call setDataReceiveHandler:withContext: for you GK session on the receiver? If you didn't do it before you start to send data.

krafter
  • 1,425
  • 1
  • 15
  • 28
2

Ensure you implement this in the header file: GKPeerPickerControllerDelegate

Devin Burke
  • 13,642
  • 12
  • 55
  • 82
D00gle
  • 21
  • 1
  • 3