如何使用gksession发送和接收数据?
你好 我使用 gksession 发送数据。代码如下。这里发送数据工作正常 并给出数据已发送的消息。但我无法在其他设备上接收任何数据 device.当我调试代码时,它没有调用并进入方法内部 接收数据。有人对此有任何想法吗?这里出了什么问题或者我该怎么办 解决问题吗?
- (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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否为接收器上的 GK 会话调用了
setDataReceiveHandler:withContext:
?如果您在开始发送数据之前没有这样做。
Did you call
setDataReceiveHandler:withContext:
for you GK session on the receiver?If you didn't do it before you start to send data.
确保在头文件中实现此功能:
GKPeerPickerControllerDelegate
Ensure you implement this in the header file:
GKPeerPickerControllerDelegate