如何在不使用GKPeerPicker的情况下通过GKSession在电话之间传输数据 [英] How to transfer data between phones over GKSession without using GKPeerPicker

查看:78
本文介绍了如何在不使用GKPeerPicker的情况下通过GKSession在电话之间传输数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个使用GKSession在2部以上手机之间传输数据的应用程序.事情有两种选择:

I'm trying to create an app that transfers data between 2+ phones using GKSession. Thing is there are two options:

第一:使用 WIFI接口.苹果没有提供有关如何执行此操作的说明:

First: using the GKPeerPicker.. However here I get stuck at the point where I have to implement my own WIFI interface.. apple provides no instructions on how to do that:

- (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:    (GKPeerPickerConnectionType)type {
    if (type == GKPeerPickerConnectionTypeOnline) {
        picker.delegate = nil;
        [picker dismiss];
        [picker autorelease];
       // Implement your own internet user interface here.
    }
}

第二个:跳过GKPeerPicker并全力以赴,就像在示例.但是,app开发人员文档没有提供有关如何在不使用GKPeerPicker的情况下如何发送/接收数据的说明.(我也无法在thew网站上找到该示例的任何示例)

Second: Skipping GKPeerPicker and doing the whole thing my self, like in this example. However the app dev documentation doesn't provide any instructions on how to send/receive data without using GKPeerPicker.. (nor could I find any example of that on thew web)

推荐答案

我刚刚想出了如何在不使用peerpicker的情况下连接设备的方法.这是个猜测游戏,因为文档尚不清楚,而且我在互联网上寻找有关此信息的时间已经很长了.我将尽力在这里解释所有内容,以解决以后任何人可能遇到的任何问题.

I just figured out how to connect devices without the peerpicker. It was a bit of a guessing game because the documentation is pretty unclear and I've looked for so long on the internet for any info about this. I'll try to explain everything here to clear up any questions anyone in the future might have.

来自文档:

GKSession对象提供发现并连接到 使用蓝牙或Wi-fi的附近iOS设备.

A GKSession object provides the ability to discover and connect to nearby iOS devices using Bluetooth or Wi-fi.

这是对我了解它的第一步.我以为GKPeerPickerController负责广告和连接,但是GKSession实际上可以完成所有这些工作.

This was the first step to understand it for me. I thought the GKPeerPickerController was responsible of the advertising and connecting but GKSession actually does all that.

要理解的第二件事是, peers 不一定与您相关.它们可以就在附近等待被发现并连接.所有同伴都有状态

The second thing to understand is that what is referred to as peers are not necessarily connected to you. They can just be nearby waiting to be discovered and connected to. All peers have a state

  • GKPeerStateAvailable(这很有用!)
  • GKPeerState不可用
  • GKPeerStateConnected
  • GKPeerStateDisconnected
  • GKPeerStateConnecting

那么我们实际上如何连接?首先,我们必须创建一个GKSession对象,以便能够找到我们周围的同伴并查看它们何时可用:

So how do we actually connect? Well first we have to create a GKSession object to be able to find peers around us and see when they become available:

// nil will become the device name
GKSession *gkSession = [[GKSession alloc] initWithSessionID:@"something.unique.i.use.my.bundle.name" displayName:nil sessionMode:GKSessionModePeer];
[gkSession setDataReceiveHandler:self withContext:nil];
gkSession.delegate = self;
gkSession.available = YES; // I'm not sure this if this is the default value, this might not be needed

现在我们有一些委托调用来响应. session:didReceiveConnectionRequestFromPeer:session:peer:didChangeState(您还应该适当地处理GKSessionDelegate的调用以进行断开连接和失败操作)

Now we have some delegate calls to respond to. session:didReceiveConnectionRequestFromPeer: and session:peer:didChangeState (you should also handle the calls of GKSessionDelegate for disconnection and failure appropriately)

-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
    if(state == GKPeerStateDisconnected)
    {
        // A peer disconnected
    }
    else if(state == GKPeerStateConnected)
    {   
        // You can now send messages to the connected peer(s)
        int number = 1337;
        [session sendDataToAllPeers:[NSData dataWithBytes:&number length:4] withDataMode:GKSendDataReliable error:nil];
    }
    else if (state == GKPeerStateAvailable)
    {
        // A device became available, meaning we can connect to it. Lets do it! (or at least try and make a request) 
        /*
        Notice: This will connect to every iphone that's nearby you directly. 
        You would maybe want to make an interface similar to peerpicker instead
        In that case, you should just save this peer in a availablePeers array and 
        call this method later on. For your UI, the name of the peer can be 
        retrived with [session displayNameForPeer:peerId]
        */
        [session connectToPeer:peerID withTimeout:10];
    }

}

对方现在收到了一个他应该回复的请求.

-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
    // We can now decide to deny or accept
    bool shouldAccept = YES;
    if(shouldAccept)
    {
        [session acceptConnectionFromPeer:peerID error:nil];
    }
    else
    {
        [session denyConnectionFromPeer:peerID];
    }
}

最后收到我们的1337小信息

-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession*)session context:(void *)context
{
    int number = 1337;
    if([data isEqualToData:[NSData dataWithBytes:&number length:4]])
    {
        NSLog(@"Yey!");
    }
}

这篇关于如何在不使用GKPeerPicker的情况下通过GKSession在电话之间传输数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆