我在Swift 4中使用Multipeer Connectivity,我想知道如何在程序运行时查看找到的设备,我得到了一些帮助,但我仍然不太了解所有这些。我也不知道在哪里放这个:
onMessageReceived
以下是我的完整代码,如果需要请更正任何代码,我也将扩展名放在下面代码的底部:
extension viewcontroller : MCBrowserViewControllerDelegate {
func browserViewControllerDidFinish(_ browserViewController:
MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController:
MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
}
我刚刚编辑了扩展程序,但它说import UIKit
import PlaygroundSupport
import MultipeerConnectivity
PlaygroundPage.current.needsIndefiniteExecution = true
var peerID: MCPeerID!
var mcSession: MCSession!
var mcAdvertiserAssistant: MCAdvertiserAssistant!
var foundPeers = [MCPeerID]()
class viewcontroller: UIViewController {
override func viewDidLoad() {
var serviceBrowser : MCBrowserViewController!
func startBrowsingForPeer() {
serviceBrowser = MCBrowserViewController(serviceType: "lightning417techa", session: mcSession)
serviceBrowser.delegate.self
}
peerID = MCPeerID(displayName: UIDevice.current.name)
mcSession = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .none)
mcSession.delegate.self
class ViewController: UIViewController, MCSessionDelegate, MCBrowserViewControllerDelegate {
let session = MCSession(peer: peerID,
securityIdentity: nil,
encryptionPreference: .none)
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
switch state {
case MCSessionState.connected:
print("Connected: \(peerID.displayName)")
case MCSessionState.connecting:
print("Connecting: \(peerID.displayName)")
case MCSessionState.notConnected:
print("Not Connected: \(peerID.displayName)")
}
}
func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!) {
foundPeers.append(peerID)
print(browser)
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
if let image = UIImage(data: data) {
DispatchQueue.main.async { [unowned self] in
print("image recieved")
}
}
}
func sendImage(img: UIImage) {
if mcSession.connectedPeers.count > 0 {
if let imageData = UIImagePNGRepresentation(img) {
do {
try mcSession.send(imageData, toPeers: mcSession.connectedPeers, with: .reliable)
} catch let error as NSError {
let ac = UIAlertController(title: "Send error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
}
}
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {
}
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
}
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
}
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
dismiss(animated: true)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
dismiss(animated: true)
}
func startHosting(action: UIAlertAction!) {
mcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "hws-ridleyn", discoveryInfo: nil, session: mcSession)
mcAdvertiserAssistant.start()
}
func joinSession(action: UIAlertAction!) {
let mcBrowser = MCBrowserViewController(serviceType: "hws-ridleyn", session: mcSession)
mcBrowser.delegate = self
present(mcBrowser, animated: true)
}
let vc = UIToolbar(frame: CGRect(x: 100, y: 100, width: 200, height: 200))
// your view controller here
}
let vcc = ViewController()
vcc.startHosting(action: nil)
ViewController.startHosting(vcc)
}
}
PlaygroundPage.current
extension viewcontroller : MCBrowserViewControllerDelegate {
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
}
某种程度上是不正确的,它想用serviceBrowser
替换它。所以当我这样做的时候,NetServiceBrowser
没有成员NetServiceBrowser
。
答案 0 :(得分:0)
MCSession包含一个名为connectedPeers的变量。
var connectedPeers:[MCPeerID] 当前连接到此会话的所有对等方的数组。
答案 1 :(得分:0)
如果您需要找到的对等项列表,请将存储的属性修改为
var foundPeers = [MCPeerID]() {
didSet {
let peers = foundPeers.map( {$0.displayName} )
yourFoundPeerLabel.text = String(format: "Found Peers: %@", peers.joined(separator: ", "))
}
}
每次找到设备时,storedProperty都会更新,并且在将新对等体追加到列表后,您的标签将自动更新。请参考您对委托方法的实现
func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!);
PS:如果需要连接的设备列表,请创建一个类似的存储属性并使用委托方法
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState);
将设备添加到列表中。逻辑保持不变。