Nanoleaf

Control your Nanoleaf devices with Swift.

Requirements

  • iOS 15.0+
  • macOS 12.0+

Installation

Swift Package Manager

In Xcode:

  1. Click Project.
  2. Click Package Dependencies.
  3. Click +.
  4. Enter package URL: https://github.com/reddavis/Nanoleaf.
  5. Add Nanoleaf to your app target.

Documentation

Documentation can be found here.

Getting Started

To connect to a Nanoleaf device, three steps are required.

Firstly use NanoleafDeviceBrowser to discover local devices with Bonjour. The NanoleafDeviceBrowser will return an array of DeviceIdentifier‘s.

let browser = NanoleafDeviceBrowser()
browser.start()

for await identifiers in browser.identifiers {
    // ...
}

If you want some useful utilties and extensions for using AsyncSequence check out Asynchrone.

Next, you will need to resolve the DeviceIdentifier into a DeviceAddress. This can be done using the aptly named DeviceAddressResolver.

let resolver = DeviceAddressResolver(identifier: identifier)
do {
    let address = try await resolver.resolve()
    // ...
} catch {
    // ...
}

Finally, you need to make a authentification request. Before executing this request, press and hold the power button on the Nanoleaf device untill the LEDs begin to flash. Now execute the AuthenticateRequest using the url of the DeviceAddress. If successful, this will return an Auth object that contains a token.

do {
    let client = NanoleafClient.main()
    let request = AuthenticateRequest(url: address.url)
    let response = try await client.execute(request: request)
    let token = response.token
} catch {
    // ...
}