pierrickrouxel / SSDPClient

SSDP client for Swift
MIT License
46 stars 16 forks source link
ssdp swift swift-package-manager

SSDPClient

gitHub license gitHub release github stable donate

SSDP client for Swift using the Swift Package Manager. Works on iOS, macOS, tvOS, watchOS and Linux.

Installation

GitHub spm

SSDPClient is available through Swift Package Manager. To install it, add the following line to your Package.swift dependencies:

.package(url: "https://github.com/pierrickrouxel/SSDPClient.git", from: "1.0.0")

Usage

SSDPClient can be used to discover SSDP devices and services :

import SSDPClient

class ServiceDiscovery {
    let client = SSDPDiscovery()

    init() {
        self.client.delegate = self
        self.client.discoverService()
    }
}

To handle the discovery implement the SSDPDiscoveryDelegate protocol :

extension ServiceDiscovery: SSDPDiscoveryDelegate {
    func ssdpDiscovery(_: SSDPDiscovery, didDiscoverService: SSDPService) {
        // ...
    }
}

Discovery

SSDPDiscovery provides two instance methods to discover services :

Delegate

The SSDPDiscoveryDelegate protocol defines delegate methods that you should implement when using SSDPDiscovery discover tasks :

Service

SSDPService is the discovered service. It contains the following attributes :

Test

Run test:

swift test

Troubleshooting

The application crash with error code -9982 Bad file descriptor

You probably run a security issue. You should grant the local network authorization access.

You can handle this error using the following code:

let authorization = LocalNetworkAuthorization()
authorization.requestAuthorization { granted in
    if granted {
        print("Permission Granted")
        let discovery = ServiceDiscovery(delegate: self)
        discovery.start()
    } else {
        print("Permission denied")
    }
}