I've been reviewing docs here: https://developer.apple.com/news/?id=0oi77447
I'm new to swift, but trying to make sense of what's listed there to create a very simple iOS app that uses multicast IP 239.6.4.50, port 5890 on a "sender device" application to send a special payload in a single multicast UDP packet to multiple Receiver Devices (running a separate app) who are all obviously joined to that ip/port to receive the multicasts. I DO Have the Multicast entitlement and have configured (and verified) my Xcode project to ensure it's compiling the "Hello world" app with the multicast entitlements correctly. ...so that's not going to be an issue.
I can't get this code figure out to compile. ... just trying to send "Hello world" as the payload of a multicast packet when the button is clicked in the view controller.
Anyone here about to point out what I'm doing wrong?
code-block
import SwiftUI
import os
import Foundation
import Network
import UIKit
class multicastController {
var multicastIP : NWEndpoint.Host = "239.6.4.50"
var multicastPort : NWEndpoint.Port = 5890
let message = "Hello World"
init() {
let endpoints: [NWEndpoint] = [.hostPort(host: multicastIP, port: multicastPort)]
guard let multicast = try? NWMulticastGroup(for: endpoints)
else { fatalError("Multicast Group setup error") }
let params: NWParameters = .udp
params.allowLocalEndpointReuse = true
let group = NWConnectionGroup(with: multicast, using: params)
group.setReceiveHandler(maximumMessageSize: 16384, rejectOversizedMessages: true) { (message, data, isComplete) in
if let data = data {
let dataString = String(data: data, encoding: .utf8)
print(message, dataString ?? "", isComplete)
} else {
print(message, data ?? "", isComplete)
}
}
group.stateUpdateHandler = { (newState) in
print("Group entered state \(String(describing: newState))")
}
group.start(queue: .main)
}
func sendMulticastPacket() {
group.send(content: data) { error in
if let error = error {
print(error.localizedDescription)
} else {
print("ok")
}
}
}
}
struct ContentView: View {
var session = multicastController()
let message = "Hello World"
var body: some View {
VStack {
Button(action: {
session.sendMulticastPacket()
}, label: {
Text("Send Multicast Packet")
}
)}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Selecting any option will automatically load the page