2011-03-14 55 views
1

你好 我使用gksession.code發送數據如下。這裏發送的數據工作正常 並給出了數據發送的消息。但我無法收到其他 設備上的任何數據。當我調試代碼時是不是打電話,並進入方法 接收data.Does任何人有任何想法關於這個?這裏有什麼不對或怎麼可以我 解決這個問題?如何使用gksession發送和接收數據?

- (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];  
    } 

回答

2

您是否在接收器上爲您的GK會話撥打setDataReceiveHandler:withContext:? 如果您在開始發送數據之前沒有做到這一點。

2

確保您在頭文件中實現了這一點:GKPeerPickerControllerDelegate