Sonos Swift Networking

License Platforms SPM Swift Version Twitter

Sonos Swift Networking is a wrapper for the Sonos API's.

Sonos Swift SDK

Want an out-of-the-box solution to integrating your app with Sonos? Check out the Sonos Swift SDK which is built on top of this library.

How To Get Started

  1. Create a Sonos Developer account here
  2. Then setup a new integration here
  3. Lastly, grab the Key Name, Key and Secret from your new integration

Installation

Sonos Swift Networking supports the following installation methods:

Swift Package Manager

In Package.swift add the following:

dependencies: [
    .package(url: "https://github.com/JimmyJammed/sonos-swift-networking", from: "1.0.0")
],
targets: [
    .target(
        name: "MyProject",
        dependencies: ["SonosNetworking"]
    )
    ...
]
Note: Sonos Swift Networking requires the following Swift Package dependencies:

AFNetworking
Mocker - for unit testing

Usage

Using Sonos Swift Networking is pretty straightforward and should look familiar to most networking requests.

Important

You will need to follow the Authorization Steps to generate an Authorization Code in order to use any of the APIs provided by Sonos Swift Networking. This library does support the Authorization Token (and Refresh Token) API's, but cannot create the Authorization Code directly, as it requires the user to interact with the Sonos Authorization URL from within a browser or web view.
You will need to implement your own solution for creating an Authorization Code.
Alternatively, you can use the Sonos Swift SDK as it solves this problem with provided support for creating Authorization Codes.

Example

Here is an example for fetching the current volume of a specified Player:

PlayerGetVolumeNetwork(accessToken: <ACCESS_TOKEN>, playerId: <PLAYER_ID>) { data in
	guard let data = data else {
		// Handle missing response data...
		return
	}
	// Success! Parse the data...
} failure: { error in
	// Handle API error response...
}.performRequest()

All API operations are documented in the codebase and have unit tests implemented, so take a look at those for further examples.

Subscribe API's

It is important to note that the subscribe/unsubscribe API operations do require a separate server to be setup in order to receive real-time events when your Sonos devices are updated.

This is not something that can be directly implemented in the client. You will need to setup a server using a provider of your choice (AWS, etc).

You can however test these API's with a quick and easy method:

  1. Create a free webhook server here.
  2. Go to your integration on the Integrations page of the Sonos Developer Portal.
  3. In the Credentials tab, set the Event Callback URL to the URL you copied in Step #2.
  4. Fire any of the subscribe API operations then make a change directly on your Sonos device and you should observe events being reported to the webhook server you setup in Step #1.
  5. These steps are only intended for testing the subscribe/unsubscribe API's and are not a viable solution for a production-ready app.

Requirements

Sonos Swift Networking Version Sonos Swift Networking Version Minimum iOS Target Minimum macOS Target Minimum watchOS Target Minimum tvOS Target Notes
1.0.0 1.0.0 iOS 14 10.12 x x Xcode 12+ is required.

Supported Sonos APIs

Here is a list of the currently supported Sonos API's:

Unit Tests

Sonos Swift Networking includes a suite of unit tests within the Tests subdirectory. You can also look at these for examples on how to use each API operation.

Contribution Guide

A guide to submit issues, to ask general questions, or to open pull requests are here.

Credits

Sonos Swift Networking is an open source project and unaffiliated with Sonos Inc.

And most of all, thanks to Sonos Swift Networking's growing list of contributors.

License

Sonos Swift Networking is released under the MIT license. See LICENSE for details.