MultipeerConnectivity 拒绝连接

Posted

技术标签:

【中文标题】MultipeerConnectivity 拒绝连接【英文标题】:MultipeerConnectivity declining connection 【发布时间】:2020-05-07 23:04:03 【问题描述】:

我正在尝试在两台设备之间建立蓝牙连接,这是我的代码:

var peerID: MCPeerID!
var mcSession: MCSession!
var mcAdvertiserAssistant: MCAdvertiserAssistant!

@IBAction func connectBlueTooth(_ sender: Any) 
    let actionSheet = UIAlertController(title: "משחק רשת", message: "מעוניין ליצור משחק חדש או להצטרף למשחק קיים?", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "יצירת משחק חדש", style: .default, handler: 
        (action: UIAlertAction) in
        self.startHosting()
    ))

    actionSheet.addAction(UIAlertAction(title: "חיפוש משחק קיים", style: .default, handler: 
        (action: UIAlertAction) in
        self.joinSession()
    ))

    actionSheet.addAction(UIAlertAction(title: "ביטול", style: .default, handler: nil))

    if let popoverController = actionSheet.popoverPresentationController 
        popoverController.sourceView = self.view
        let constraintValue = AppConfigurator.getConstraintByDevice()

        popoverController.sourceRect = CGRect(x: self.view.bounds.maxX - constraintValue, y: self.view.bounds.maxY - (3.5 * constraintValue), width: 0, height: 0)
        popoverController.permittedArrowDirections = []
    

    self.present(actionSheet, animated: true, completion: nil)

还有扩展:

extension MainMenuVC: MCBrowserViewControllerDelegate 
    func browserViewControllerDidFinish(_ browserViewController: MCBrowserViewController) 
        dismiss(animated: true, completion: nil)
    

    func browserViewControllerWasCancelled(_ browserViewController: MCBrowserViewController) 
        dismiss(animated: true, completion: nil)
    


extension MainMenuVC: MCSessionDelegate 
        func startHosting() 
            mcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "CatchTheDragon", discoveryInfo: nil, session: mcSession)
            mcAdvertiserAssistant.start()
        

        func joinSession() 
            let mcBrowser = MCBrowserViewController(serviceType: "CatchTheDragon", session: mcSession)
            mcBrowser.delegate = self
            present(mcBrowser, animated: true)
        

        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 session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) 
            let userInfo = ["data" : data, "peerID": peerID] as [String : Any]
            DispatchQueue.main.async 
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "WPC_DidReceiveDataNotification"), object: nil, userInfo: userInfo)
            
        

        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?)    

由于某种原因,设备卡在“正在连接”状态,最终解决为“拒绝”状态。我不喜欢使用MCNearbyServiceAdvertiserDelegateMCNearbyServiceBrowser。我在这里错过了什么?

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,但我找到了以下文章并进行了修复:"Multipeer Сonnectivity Framework in Swift: tutorial"

无法通过其他设备获得加入会话的提醒?

嗯,这是 XCode 11.2 和 ios 13 中出现并且仍然存在的常见问题。我们不知道 Apple 是否发现该框架已过时或稍后会修复它。 其中一种解决方案可能是 - 从使用 SceneDelegate 和 AppDelegate 降级为仅一个类:AppDelegate 并为未来的应用使用 UIScene ,而不是这一类。

首先,从 Info.plist 中彻底删除“Application Scene Manifest”条目; 移除 SceneDelegate 类,并移除 AppDelegate 中所有与场景相关的方法; 如果缺少,添加属性 var window: UIWindow?到您的 AppDelegate 类。

您的应用现在应该只使用应用委托,并且在 iOS 13 下,它将具有与 iOS 12 相同的生命周期。多点连接应该按照最初的设计工作。

【讨论】:

以上是关于MultipeerConnectivity 拒绝连接的主要内容,如果未能解决你的问题,请参考以下文章

Peer(MultiPeer Connectivity)已连接但未存储在 connectedPeers

Multipeer Connectivity 自动静默连接

无法通过 Multipeer Connectivity 连接蓝牙

框架未正确导入? 'MultipeerConnectivity' 上的 Mach-O 链接器

iOS MultipeerConnectivity

iOS 和 Windows 之间的 MultipeerConnectivity