如何使用gksession发送和接收数据?

发布于 2024-10-21 16:39:30 字数 1853 浏览 2 评论 0原文

你好 我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-10-28 16:39:31

您是否为接收器上的 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.

当梦初醒 2024-10-28 16:39:31

确保在头文件中实现此功能:GKPeerPickerControllerDelegate

Ensure you implement this in the header file: GKPeerPickerControllerDelegate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文