我一直在尝试让MCBrowserViewController出现,但是没有成功。我正在为iOS开发的应用程序中尝试创建它。有人可以帮忙吗?这是我的代码:
import UIKit
import PlaygroundSupport
import MultipeerConnectivity
var peerID: MCPeerID!
var mcSession: MCSession!
var mcAdvertiserAssistant: MCAdvertiserAssistant!
var foundPeers = [MCPeerID]()
PlaygroundPage.current.needsIndefiniteExecution = true
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)
browser.startBrowsingForPeers()
}
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) {
dismiss(animated: true, completion: nil)
}
func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) {
dismiss(animated: true, completion: nil)
}
}
PlaygroundPage.current
如果你在想,我已经做了很多研究,但我仍然不太明白这个问题,我也看过苹果的文档,但那里没有给我一个合适的例子。我之前问过这样的问题,但是我没有得到一个可以参考的答案。所以如果你可以,请发表你的答案,并附上我的当前代码。