iOS UDP Broadcast (Swift)
Apply for the Multicast Networking capability
To use UDP broadcast in iOS, you must submit a Multicast Networking Entitlement Request application to Apple using the link below:
https://developer.apple.com/contact/request/networking-multicast
Sample information you need to provide
APP Name : XXXXX
App Store URL : https://apps.apple.com/us/app/xxxxxxxxx
Apple ID : 1234567890
App Category : the APP’s categoryDescribe the main purpose of your app:
The app helps users to setup our company’s router products.Explain why your app needs to send multicast:
Our APP uses the UDP broadcast to discover our company’s devices.UDP IP : 255.255.255.255
Port : 12345
Waiting for serval days, if your application is approved by Apple, you will see the Multicast Networking capability in your APP identifier’s Additional Capabilities.
UDP broadcast via CocoaAsyncSocket
My sample project source code in GitHub
Please note: In order to run the sample project, you need to use the iPhone simulator. If you try to run the sample project on a real phone, you will need to have “Multicast Networking capability”.
Pod install
1 | pod 'CocoaAsyncSocket' |
If you use XCode 13+, set the project format to avoid pod init error
Start broadcast
1 | func startBroadcast(port:Int, data:Data) -> Bool{ |
Stop broadcast
1 | func stopBroadcast(){ |
Handle delegate
1 | extension UDPBroadcast: GCDAsyncUdpSocketDelegate{ |
UDP broadcast via NWConnection
Technically, NWConnection can be used to perform UDP broadcast. However, in my experience, I have not been able to get it to work.