首先申請Multicast Networking Entitlement Request權限
以下內容轉載:https://www.yuque.com/docs/share/b9d0f6fe-0428-4a9a-b9a9-218d244f98c2?# 《iOS14 廣播組播發送失敗問題》
1. 簡單說明
項目使用到了CocoaAsyncSocket,建立TCP之前,使用了UDP廣播獲取IP地址,但是系統升級到iOS 14之后,發現有台iPad間歇性可以收到廣播,iPhone一直沒有收到廣播。
2. 解決辦法
- Info.plist添加NSLocalNetworkUsageDescription
- 發送一次UDP廣播,觸發權限彈框,讓用戶點擊好,允許訪問本地網絡。
- (void)setupUdpSocket {
NSError *error = nil;
GCDAsyncUdpSocket *socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
self.udpSocket = socket;
[socket setIPv4Enabled:YES];
[socket setIPv6Enabled:NO];
[socket bindToPort:udpPort error:&error];
// 關鍵一行代碼,發送廣播,即使應用不需要。如果不發送,無法觸發是否允許訪問本地網絡彈框。
[self.udpSocket sendData:[@{} mj_JSONData] toHost:@"255.255.255.255" port:udpPort withTimeout:10 tag:100];
[socket enableBroadcast:YES error:&error];
[socket beginReceiving:&error];
}
- 如果上面兩步不生效,或則在正式版本不生效,繼續往下看。
- 到這個頁面申請APP接受組播或廣播的權限Multicast Networking Entitlement Request
Describe the main purpose of your app(for reference only):
A smart feeding device. After the app is connected to the device, the app can be used to remotely control the device, allowing the device to automatically deliver food, and you can set the time for the device to automatically deliver food. Users can also remote video.
Explain why your app needs to send multicast or broadcast traffic, or browse for all Bonjour service types
(for reference only:)
This project uses CocoaAsyncSocket. Before TCP is established, UDP broadcast is used to obtain the IP address. However, after the system was upgraded to iOS 14, it was found that some devices could not receive the broadcast and users could not connect to the product device. I hope that I can apply for the APP to accept multicast or broadcast permissions.
- 如果你的APP需要發送UDP廣播,需要先發送一個偽包來觸發彈框(本地網絡訪問),讓用戶確定后,再開始發送UDP廣播。如果只是接受廣播,不用處理
- 之后Apple通過郵件回復
// 官方回復要求我再描述的詳細一點,其實我描述的已經很清楚了,但是用的是中文,所以還是乖乖用英文具體描述一下吧。 主要用來獲取IP地址,端口號已經協商好了。
Hello,
Thank you for your interest in Multicast Networking. Can you please provide more details on which multicast or broadcast protocol (IP address and port) you need to use? What messages are exchanged between devices?
Specifically, it seems as though this type of device discovery could be accomplished via Bonjour, which doesn’t need the entitlement.
Best regards,
Apple Networking
- 最后通過后,Apple回復已通過,參考Instructions for using the entitlement (使用New Process)配置即可。
主要配置流程為:
- 登錄開發者網站
- 在 Certificates, Identifiers & Profiles, 選擇 Identifiers
- 選擇你想設置的應用Id
- 滑動到最后Additional Capabilities --> Multicast Networking 勾選后保存
- 接下來的全部可以省略(下文解釋)
3. xcode本地配置.entitlements文件
增加一個key:com.apple.developer.networking.multicast 類型是boolean YES
.entitlements 文件文件可以在 Xcode build setting 中的 code signing entitlements 中設置路徑。