我一直试图让Multipeer连接在SWIFT 4中工作,但是我无法查看MCBrowserViewController,我是swift的新手,所以任何人都可以提供帮助吗?我也打算做到这一点,https://www.hackingwithswift.com/example-code/system/how-to-create-a-peer-to-peer-network-using-the-multipeer-connectivity-framework 因此,我的目标是以编程方式建立点对点连接。
这是我的代码:
import UIKit
import PlaygroundSupport
import MultipeerConnectivity
var peerID: MCPeerID!
var mcSession: MCSession!
var mcAdvertiserAssistant: MCAdvertiserAssistant!
var browser: MCNearbyServiceBrowser!
var foundPeers = [MCPeerID]()
class viewcontroller: UIViewController {
override func viewDidLoad() {
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
func presentViewControllerAsSheet(_ viewController: MCBrowserViewController) {
}
答案 0 :(得分:0)
您没有获得BrowserViewController,因为您尚未声明它。 删除MCNearbyServiceBrowser。
<强>步骤:强>
声明 MCBrowserViewController
类型的变量 var serviceBrowser : MCBrowserViewController!
然后开始浏览同伴
func startBrowsingForPeer() {
serviceBrowser = MCBrowserViewController(serviceType: locationServiceType, session: session)
serviceBrowser.delegate = self
}
当您想要显示MCBrowserViewController时调用startBrowsingForPeer()
实施 MCBrowserViewControllerDelegate 及其功能
extension viewcontroller : MCBrowserViewControllerDelegate {
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
}
答案 1 :(得分:0)
现在是我的代码。它说在扩展中,serviceBrowser有错误。它不断纠正NetServiceBrowser。
import UIKit
import PlaygroundSupport
import MultipeerConnectivity
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: "hws- ridleyn", 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)
}
}
extension viewcontroller : MCBrowserViewControllerDelegate {
func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
serviceBrowser.dismiss(animated: true, completion: nil)
}
}
PlaygroundPage.current