我正在开发一款需要Multipeer Connectivity并遇到特殊问题的应用。
如果我设置MCSessionDelegate,我的设备不会连接。如果我没有设置MCSession委托我的设备连接,但我无法收到回复。
没有委托,我可以告诉他们正在连接,因为打印出MCSession.connectedPeers
数组会显示其他同伴。
处理连接和通信的类的简化版本如下所示。
import UIKit
import MultipeerConnectivity
class ConnectionManager: NSObject, MCSessionDelegate, MCNearbyServiceBrowserDelegate, MCNearbyServiceAdvertiserDelegate {
// MARK: - Properties
var advertiser: MCNearbyServiceAdvertiser!
var browser: MCNearbyServiceBrowser!
var peerID: MCPeerID!
var session: MCSession!
// MARK: - Lifecycle
override init() {
super.init()
peerID = MCPeerID(displayName: UIDevice.current.name)
advertiser = MCNearbyServiceAdvertiser(peer: peerID, discoveryInfo: nil, serviceType: "testconnect")
advertiser.delegate = self
browser = MCNearbyServiceBrowser(peer: peerID, serviceType: "testconnect")
browser.delegate = self
session = MCSession(peer: peerID, securityIdentity: nil, encryptionPreference: .optional)
session.delegate = self
}
deinit {
advertiser.stopAdvertisingPeer()
browser.stopBrowsingForPeers()
print("Stopping connection service...")
}
// MARK: Session Delegate
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
}
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
switch state {
case .connected:
print("\(peerID.displayName) connected")
case .notConnected:
print("\(peerID.displayName) not connected")
case .connecting:
print("\(peerID.displayName) connecting")
}
}
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, didReceiveCertificate certificate: [Any]?, fromPeer peerID: MCPeerID, certificateHandler: @escaping (Bool) -> Void) {
}
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
}
// MARK: - Nearby Advertiser Delegate
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) {
print("\(#function): \(error.localizedDescription)")
}
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
print("Invitation received from \(peerID.displayName)")
invitationHandler(true, session)
}
// MARK: - Nearby Browser Delegate
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
print("Lost peer \(peerID.displayName)")
}
func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) {
print(error.localizedDescription)
}
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String: String]?) {
print("Found peer \(peerID.displayName)")
browser.invitePeer(peerID, to: session, withContext: nil, timeout: 30)
}
}
如果我注释掉设备连接的session.delegate = self
行,如打印session.connectedPeers所示。保持原样,设备尝试连接,但失败。
我还创建了一个非常简单的应用程序,用于演示此问题,https://github.com/StagasaurusRex/MultiPeerConnectionTest
只需要让两个设备都进行广告宣传,然后让其中一个设备进行浏览,设备就会尝试连接,您可以看到代理服务器失败了,但没有它们就会成功。通过打印连接来验证这一点。
有谁知道发生了什么事?我非常感谢一些帮助,我一直在试图弄清楚我的头发。
感谢。
答案 0 :(得分:0)
好吧我明白了。
我有一个空的session实现(_:didReceiveCertificate:fromPeer:certificateHandler :)拒绝连接,因为它没有调用带有true的certificateHandler。
删除该空函数解决了我的问题并允许我的设备连接。