upnpx
VLC でも使われているとか
https://code.google.com/archive/p/upnpx/
https://github.com/fkuehne/upnpx
CocoaPods でインストール
pod "upnpx", "~> 1.4.0"
UPnPデバイスの検索
UPnPDB に UPnPDBObserver を追加し、ssdp の searchSSDP を呼ぶ。
import upnpx
class RootViewController: UITableViewController {
var devices: [BasicUPnPDevice] = [] {
didSet {
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
searchUPnPDevices()
}
var upnpDB: UPnPDB?
func searchUPnPDevices() {
guard upnpDB == nil else { return }
guard let db = UPnPManager.getInstance().db else { return }
db.add(self)
upnpDB = db
_ = UPnPManager.getInstance().ssdp.searchSSDP
}
...
}
UPnPDBObserver プロトコルを実装し、デバイス検出の通知を受けとる。
extension RootViewController: UPnPDBObserver {
func uPnPDBWillUpdate(_ db: UPnPDB!) {
print(#function)
}
func uPnPDBUpdated(_ db: UPnPDB!) {
print(#function)
if let devices = db.rootDevices as? [BasicUPnPDevice] {
self.devices = devices
}
}
}
画面表示はよしなに。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devices.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "device", for: indexPath)
let device = devices[indexPath.row]
cell.textLabel?.text = device.friendlyName
cell.detailTextLabel?.text = device.baseURL.host
if device as? MediaServer1Device != nil {
cell.accessoryType = .disclosureIndicator
} else {
cell.accessoryType = .none
}
return cell
}
結果
つづく