iOS14 广播组播发送失败问题
首先申请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 中设置路径。