LoginSignup
2
4

More than 5 years have passed since last update.

iOS upnpx で UPnP デバイスをブラウズ(その1 デバイス発見まで)

Posted at

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
    }

結果

スクリーンショット 2018-01-09 0.34.33.png

つづく

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4