環(huán)境: xcode15,iOS14+
使用MultipeerConnectivity或NSNetServiceBrowser時報錯,錯誤信息如下:
Server did not publish: errorDict [{
NSNetServicesErrorCode = "-72008";
NSNetServicesErrorDomain = 10;
}].
解決方案:
需要在info.plist加入
-
NSLocalNetworkUsageDescription- 權(quán)限申請 -
NSBonjourServices- 服務(wù)聲明
<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>
注意:
plist的NSBonjourServices,按照你的需求選擇是否tcp和udp都有,
命名規(guī)則是_my-service._tcp和_my-service._udp,
_my-service、_tcp、_udp的_都不能刪除,否則無效。
例如:
如果你定義的服務(wù)是:
<string>_foobar._tcp</string>
<string>_foobar._udp</string>
在代碼調(diào)用是就要使用foobar:
mpcAdvertiserAssistant = MCAdvertiserAssistant(serviceType: "foobar", discoveryInfo: discoveryInfoDict, session: mpcSession)