Sonos Swift Networking is a wrapper for the Sonos API's.
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.
- Create a Sonos Developer account here
- Then setup a new integration here
- Lastly, grab the Key Name, Key and Secret from your new integration
Sonos Swift Networking supports the following installation methods:
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"]
)
...
]
AFNetworking
Mocker - for unit testing
Using Sonos Swift Networking is pretty straightforward and should look familiar to most networking requests.
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.
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.
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:
- Create a free webhook server here.
- Go to your integration on the Integrations page of the Sonos Developer Portal.
- In the Credentials tab, set the Event Callback URL to the URL you copied in Step #2.
- 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. - These steps are only intended for testing the
subscribe/unsubscribe
API's and are not a viable solution for a production-ready app.
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. |
Here is a list of the currently supported Sonos API's:
- Authorization
- Control API
- Audio Clip
- cancelAudioClip
- loadAudioClip
- subscribe
- unsubscribe
- Favorites
- getFavorites
- loadFavorites
- subscribe
- unsubscribe
- Groups
- createGroup
- getGroups
- modifyGroupMembers
- setGroupMembers
- subscribe
- unsubscribe
- Group Volume
- getVolume
- setMute
- setRelativeVolume
- setVolume
- subscribe
- unsubscribe
- Home Theater
- getOptions
- loadHomeTheaterPlayback
- setOptions
- setTvPowerState
- Households
- getVolume
- setMute
- setRelativeVolume
- setVolume
- subscribe
- unsubscribe
- Music Service Accounts
- match
- Playback
- getPlaybackStatus
- loadLineIn
- pause
- play
- seek
- seekRelative
- setPlayModes
- skipToNextTrack
- skipToPreviousTrack
- togglePlayPause
- subscribe
- unsubscribe
- Playback Metadata
- getMetaDataStatus
- subscribe
- unsubscribe
- Playback Session
- createSession
- joinSession
- joinOrCreateSession
- loadCloudQueue
- loadStreamUrl
- refreshCloudQueue
- seekRelative
- skipToItem
- suspend
- subscribe
- unsubscribe
- Player Volume
- getVolume
- setMute
- setRelativeVolume
- setVolume
- subscribe
- unsubscribe
- Playlists
- getPlaylist
- getPlaylists
- loadPlaylist
- subscribe
- unsubscribe
- Settings
- getPlayerSettings
- setPlayerSettings
- subscribe
- unsubscribe
- Audio Clip
- Cloud Queue API
- TBD
- Sonos Music API
- TBD
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.
A guide to submit issues, to ask general questions, or to open pull requests are here.
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.
Sonos Swift Networking is released under the MIT license. See LICENSE for details.