多点连接总是无法连接

问题描述 投票:0回答:1

我正在尝试制作 2 个应用程序:macOS 应用程序应将数据发送到 iOS 应用程序。 他们都看到对方,开始联系,但每次几秒钟后就会断开连接。 当我制作 SwiftUI 应用程序时,我创建了 MultipeerManager:

final class MultipeerManager: NSObject, ObservableObject {
    private let serviceType = "desview"
    let localPeerID: MCPeerID
    private(set) var session: MCSession
#if os(macOS)
    private(set) var advertiser: MCNearbyServiceAdvertiser
#elseif os(iOS)
    private(set) var browser: MCNearbyServiceBrowser
#endif

#if os(macOS)
    override init() {
        self.localPeerID = MCPeerID(displayName: "DC - \(UUID().uuidString.prefix(4))")
        self.advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: ["name": "macOS design capturer"], serviceType: serviceType)
        self.session = MCSession(peer: localPeerID, securityIdentity: nil, encryptionPreference: .optional)
        super.init()
        advertiser.delegate = self
        session.delegate = self
    }
#elseif os(iOS)
    override init() {
        self.localPeerID = MCPeerID(displayName: "DV - \(UUID().uuidString.prefix(4))")
        self.browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: serviceType)
        self.session = MCSession(peer: localPeerID, securityIdentity: nil, encryptionPreference: .optional)
        super.init()
        browser.delegate = self
        session.delegate = self
    }
#endif
}

对于我设置会话的行,我尝试了所有三个选项:.none、.Optional、.required(这没有任何改变)

在 macOS 应用程序上,它只是开始做广告:

advertiser.startAdvertisingPeer()

在iPad上它开始浏览:

browser.startBrowsingForPeers()

...当找到 macOS 主机时,它会尝试连接(我存储对等点并在 UI 上选择所需的点):

func connect(to peer: MCPeerID) { // where peer is the macOS peerID
     connectionStatus = "Connecting to \(peer.displayName)"
     browser.invitePeer(peer, to: session, withContext: nil, timeout: 10)
     browser.stopBrowsingForPeers()
}

之后广告商接受邀请(我可以通过调试到达此):

func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
    invitationHandler(true, session)
}

我可以看到会话状态发生变化,但它永远不会达到.connected状态:

func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
    switch state {
    case .notConnected:
        self.connectionStatus = "Disconnected from \(peerID.displayName)"
    case .connecting:
        self.connectionStatus = "Connecting to \(peerID.displayName)"
    case .connected:
        self.connectionStatus = "IT'S CONNECTED TO: \(peerID.displayName)!!!!"
    @unknown default:
        assertionFailure("Unhandled connection state")
    }
}

我什至尝试始终允许握手(但它永远不会到达这行代码):

func session(_ session: MCSession, didReceiveCertificate certificate: [Any]?, fromPeer peerID: MCPeerID, certificateHandler: @escaping (Bool) -> Void) {
    certificateHandler(true)
}

这是 Info.plist 文件(这些行在这 2 个应用程序中重复):

<dict>
    <key>NSBonjourServices</key>
    <array>
        <string>_desview._tcp</string>
        <string>_desview._udp</string>
    </array>
</dict>

我的另一个尝试是在 Xcode 中创建 2 个独立的项目,但其行为是相同的。我更新了 macOS 和 iPadOS (+iOS) 系统,没有任何变化。我在 Xcode 15 和 Xcode 15.3 上尝试过 - 结果也是一样的。 我还能尝试什么?是否可以通过这个框架连接macOS和iOS应用程序?

ios swift macos multipeer-connectivity
1个回答
0
投票

好吧,经过几个小时的调查,发现解决方案非常简单:我只为应用程序沙箱添加了“传入连接(服务器)”功能,还需要选中传出连接:

© www.soinside.com 2019 - 2024. All rights reserved.