NSNetServiceBrowser 在 iOS 14 上没有搜索错误 -72008

DDP*_*DDP 7 networking bonjour ios multipeer-connectivity

设置多点连接(使用 Bonjour)也会触发相同的错误。我用于启动 Bonjour 浏览和 Multipeer Connectivity 的代码是从 Apple 示例代码修改而来的,并且在 iOS 13 下运行良好。

DDP*_*DDP 12

您需要将以下键添加到 Info.plist: NSLocalNetworkUsageDescriptionNSBonjourServices。例如

<key>NSLocalNetworkUsageDescription</key>
<string>Reason for using Bonjour that the user can understand</string>
<key>NSBonjourServices</key>
<array>
    <string>_my-service._tcp</string>
    <string>_my-service._udp</string>
</array>
Run Code Online (Sandbox Code Playgroud)

确保为您的服务名称正确命名了 my-service。例如,如果您正在为“foobar”服务设置 MPC ,您可能有

mpcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "foobar", discoveryInfo: discoveryInfoDict, session: mpcSession)
Run Code Online (Sandbox Code Playgroud)

所以你会使用

<string>_foobar._tcp</string>
<string>_foobar._udp</string>
Run Code Online (Sandbox Code Playgroud)

(您的实现中可能不需要 TCP 和 UDP。)

https://developer.apple.com/videos/play/wwdc2020/10110/

https://developer.apple.com/forums/thread/653316


huu*_*ang 5

我通过将以下代码添加到 Info.plist 解决了这个问题:

<key>NSBonjourServices</key>
<array>
    <string>_http._tcp.</string>
</array>
Run Code Online (Sandbox Code Playgroud)