iosswiftnetwork-connectionnetwork.framework

send udp socket multicast address in network framework iOS 17 version


I am trying send data multicast address via using NWConnectionGroup, I tried below code at iOS 15 version it works, it sends data and I received sended data, but When I try at iOS 17 version it connects successfully url and port, but it doesn't sending data, Why I can't send data from iOS 17 version, Can I need get specific permission? meanwhile I tested in simulator at iOS 17.2 version it sends data but real device can't send data.

func init() {
      var group: NWConnectionGroup!
      connect(withHost: "235.10.10.100", port: 3333)
    }

    func connect(withHost: NWEndpoint.Host, port: NWEndpoint.Port) {
        
        let multicast = try! NWMulticastGroup(for: [.hostPort(host: withHost, port: port)])
        group = NWConnectionGroup(with: multicast, using: .udp)

        group.setReceiveHandler(maximumMessageSize: 128) { message, data, b in
            print("Received message:")
        }
        group.stateUpdateHandler = { (newState) in
            print("Group entered state \(String(describing: newState))")
        }
        group.start(queue: .global())
    }

func sendData() {
    let groupSendContent = Data("helloAll".utf8)
    self.group.send(content: groupSendContent) { error in (error)
        print("Send complete with error \(String(describing: error))")
    }
}

Solution

  • Use of NWMulticastGroup requires the com.apple.developer.networking.multicast entitlement. There is a link for requesting it in the documentation.

    I do not recommend just modifying the entitlement file by hand. It's tricky to do correctly. I recommend adding it in Signing & Capabilities. Add it through the "+ Capability" button:

    Multicast Networking Capability addition screen

    Note that you will need to have this entitlement on your provisioning profile. You need to request it from Apple using the link described above.