使用Multipeer Connectivity framework,设备之间可以通过infrastructure Wi-Fi networks, peer-to-peer Wi-Fi, and Bluetooth personal area networks进行通信。
When working with the Multipeer Connectivity framework, your app must interact with several types of objects, as described below.
Session objects (MCSession
) :提供设备间的通信服务,如果创建了一个session,可以邀请其他的设备加入网络中,或者被其他设备邀请加入。
Advertiser objects (MCNearbyServiceAdvertiser
) :表示设备是可以被邀请加入特定的session.
Advertiser assistant objects (MCAdvertiserAssistant
) :功能与MCNearbyServiceAdvertiser类似,只是额外提供了接受邀请时的标准交互借口。
Browser objects (MCNearbyServiceBrowser
) :搜索附近的特定session类型的设备。
Browser view controller objects (MCBrowserViewController
): 提供标准的接口,让用户能够选择附近的设备加入一个session。
Peer IDs (MCPeerID
): uniquely identify an app running on a device to nearby peers.
Session objects 保持一组 连接到该session的peer ID objects。 Advertiser objects also use a single local peer object to provide information that identifies the device and its user to other nearby devices.
框架使用过程: the discovery phase, the session phase.
In the discovery phase, your app uses a browser object (described in MCNearbyServiceBrowser Class Reference) 来搜索附近的设备, 最好采用 MCBrowserViewController (MCBrowserViewController Class Reference) 进行交互。
The app also uses an advertiser object (described in MCNearbyServiceAdvertiser Class Reference) or an advertiser assistant object (described in MCAdvertiserAssistant Class Reference) ,表示这个设备是有效的,其他的设备可以邀请其加入session.
During the discovery phase, your app has limited communication with and knowledge of other peers; it has access to the discoveryInfo
data that other nearby clients provide, and any context data that other peers provide when inviting it to join a session.
在发现阶段,app不能访问其他的设备,只有被邀请加入到session之后,才能访问discoveryInfo data和其他的context data.
After the user chooses which peers to add to a session, the app invites those peers to join the session. Apps running on the nearby devices can choose whether to accept or reject the invitation, and can ask their users for permission.
If the peer accepts the invitation, the browser establishes a connection with the advertiser and the session phase begins. In this phase, your app can perform direct communication to one or more peers within the session. The framework notifies your app through delegate callbacks when peers join the session and when they leave the session.
如果设备接受了邀请,那么browser object与advertiser object建立连接,开始session phase。在会话阶段,设备可以与sesion中的其他一个或多个设备进行直接通信。framework会在设备加入或者离开session时通知你的app.
An MCSession
object enables and manages communication among all peers in a Multipeer Connectivity session.
To set up a session, your app must do the following:
Create an MCPeerID
object that represents the local peer, and use it to initialize the session object.
Add peers to the session using a browser object, a browser view controller, or manually.
Wait until the session calls your delegate object’s session:peer:didChangeState:
method with MCSessionStateConnected
as the new state, along with an object that tells you which peer became connected.
You should also set up an advertiser or advertiser assistant to allow other devices to ask your app to join a session that they create.
建立session后,就可以向其他的设备发送数据,方法如下:
sendData:toPeers:withMode:error:
向指定的设备发送 NSData
object
On each recipient device, the delegate object’s session:didReceiveData:fromPeer:
method is called with the data object when the data has been fully received.
在接收端,当数据全部接收后,会调用代理的session:didReceiveData:fromPeer:方法。
sendResourceAtURL:withName:toPeer:withCompletionHandler:
发送一个NSURL
object 到指定设备. The URL can be either a local file URL or a web URL. The completionHandler
block is called when the resource is fully received by the recipient peer or when an error occurs during transmission.
This method returns an NSProgress
object that you can use to cancel the transfer or check the current status of the transfer.
这个方法返回一个NSProgress 对象,可以使用这个对象取消传输过程或者检查当前传输状态。
On the recipient device, the session calls its delegate object’s session:didStartReceivingResourceWithName:fromPeer:withProgress:
method when the device begins receiving the resource, and calls itssession:didFinishReceivingResourceWithName:fromPeer:atURL:withError:
method when the resource has been fully received or when an error occurs.
在接收端,接收数据的时候,会调用代理的session:didStartReceivingResourceWithName:fromPeer:withProgress:方法;接收完成时或者出错时,
调用session:didFinishReceivingResourceWithName:fromPeer:atURL:withError:方法。
startStreamWithName:toPeer:error:
creates a connected byte stream (NSOutputStream
) that you can use to send data to the specified peer.
On the recipient device, the session calls its delegate object’s session:didReceiveStream:withName:fromPeer:
method with an NSInputStream
object that represents the other endpoint of communication.
On both sides, your code must set the stream’s delegate, schedule the stream on a run loop, and open the stream. Your code must also implement stream delegate methods to manage sending and receiving stream data.
Important: Delegate calls occur on a private operation queue. If your app needs to perform an action on a particular run loop or operation queue, its delegate method should explicitly dispatch or schedule that work.