ably/ably-asset-tracking-swift
iOS client SDKs for the Ably Asset Tracking service.
Installation
Requirements
These SDKs support support iOS and iPadOS. Support for macOS/ tvOS may be developed in the future, depending on interest/ demand.
- Publisher SDK: iOS 13.0+ / iPadOS 13.0+
- Subscriber SDK: iOS 13.0+ / iPadOS 13.0+
- Xcode 14.0+ (we currently test using Xcode 14.3.1)
- Swift 5.7+
Mapbox setup
In order to install the Ably Asset Tracking SDKs, you need to first configure your development machine so that it has permissions to download the Mapbox Navigation SDK. To do this, follow the instructions under "Configure your secret token" in the Mapbox installation guide in order to set up your ~/.netrc file.
Swift Package Manager
- To install this package in an Xcode Project:
- Paste https://github.com/ably/ably-asset-tracking-swift in the "Swift Packages" search box. (Xcode project > Swift Packages.. > + button) - Select the relevant SDK for your target. (Publisher SDK, Subscriber SDK or both) - This apple guide explains the steps in more detail.
- To install this package into a Swift Package, add the following to your manifest (
Package.swift):
``swift .package(url: "https://github.com/ably/ably-asset-tracking-swift", from: LATEST_VERSION), ``
You can find the version on the releases page.
Example Apps
- If you have not already, configure your development machine so that it has permissions to download the Mapbox Navigation SDK.
- An
Examples/Secrets.xcconfigfile containing credentials (keys/ tokens) is required to build the example apps. (You can use the exampleExamples/Example.Secrets.xcconfig, e.g. by runningcp Examples/Example.Secrets.xcconfig Examples/Secrets.xcconfig). Update the following values inExamples/Secrets.xcconfig:
- ABLY_API_KEY: Used by all example apps to authenticate with Ably using basic authentication. Not recommended in production, and can be taken from here. - MAPBOX_ACCESS_TOKEN: Used to access Mapbox Navigation SDK/ APIs, and can be taken from here. Using the Mapbox token is only required to run the Publisher example apps.
- To run the example apps, open
AblyAssetTracking.xcworkspaceand then run thePublisherExampleorSubscriberExampletarget.
AWS S3 support in publisher app (optional)
The publisher example app is able to fetch a location history file from an AWS S3 bucket and use it to replay previously-recorded journeys. To enable this functionality, you need to place an Amplify configuration file at Examples/PublisherExample/PublisherExample/Optional Resources/amplifyconfiguration.json. The example app is configured to use Cognito for auth and S3 for storage.
Running example apps on a real device
No additional setup is required when running the example apps on simulators, but if you try to run them on a real device you'll run into code signing errors.
To run the example apps on a device you'll need to change the code signing settings in Xcode:
- Select the
PublisherExampleorSubscriberExamplein the top of the Project Navigator. - Select the
PublisherExampleorSubscriberExampletarget from the targets list. - Select the
Signing & Capabilitiestab. - By default the
Teamoption is set tonone. You'll need to change it, preferably to a... (Personal team). - Since the default
bundle identifieris already being used by Ably, you won't be able to reuse it in a different team. You'll need to change it to any other value that's not already in use on App Store Connect.
If you run into an error The operation couldn’t be completed. Unable to launch ... because it has an invalid code signature, inadequate entitlements or its profile has not been explicitly trusted by the user. you'll need to "trust" your development account on your device:
Iphone Settings -> General -> VPN & Device Management -> Your development profileUsage
Publisher SDK
The Asset Publisher SDK can efficiently acquire the location data on a device and publish location updates to other subscribers in realtime. Here is an example of how the Asset Publisher SDK can be used:
import AblyAssetTrackingPublisher
// Initialise a Publisher using the mandatory builder methods
publisher = try? PublisherFactory.publishers() // get a Publisher Builder
.connection(ConnectionConfiguration(apiKey: ABLY_API_KEY, clientId: CLIENT_ID)) // provide Ably configuration with credentials
.delegate(self) // provide delegate to handle location updates locally if needed
.start()
// Start tracking an asset
publisher?.track(trackable: Trackable(id: trackingId)) // provide a tracking ID of the assetSee the PublisherBuilder protocol for addtional optional builder methods.
Subscriber SDK
The Asset Subscriber SDK can be used to receive location updates from a publisher in realtime. Here is an example of how Asset Subscribing SDK can be used:
import AblyAssetTrackingSubscriber
// Initialise a Subscriber using the mandatory builder methods
subscriber = try? SubscriberFactory.subscribers() // get a Subscriber Builder
.connection(ConnectionConfiguration(apiKey: ABLY_API_KEY, clientId: CLIENT_ID)) // provide Ably configuration with credentials
.trackingId(trackingId) // provide a Tracking ID for the asset to be tracked
.delegate(self) // provide delegate to handle location updates locally if needed
.start() // start listening to updatesSee the SubscriberBuilder protocol for addtional optional builder methods.
Authentication
Both Subscriber and Publisher SDK support basic authentication (API key) and token authentication. Specify this by passing a ConnectionConfiguration to the Subscriber or Publisher builder: SubscriberFactory.subscribers().connection(connectionConfiguration).
To use basic authentication, set the following ConnectionConfiguration on the Subscriber or Publisher builder:
let connectionConfiguration = ConnectionConfiguration(apiKey: ABLY_API_KEY, clientId: clientId)
To use token authentication, you can pass a closure which will be called when the Ably client needs to authenticate or reauthenticate:
let connectionConfiguration = ConnectionConfiguration() { tokenParams, resultHandler in
// Implement a request to your servers which provides either a TokenRequest (simplest), TokenDetails, JWT or token string.
getTokenRequestJSONFromYourServer(tokenParams: tokenParams) { result in
switch result {
case .success(let tokenRequest):
resultHandler(.success(.tokenRequest(tokenRequest)))
case .failure(let error):
resultHandler(.failure(error))
}
}
}Ably Asset Tracking UI (Location Animator)
The Location Animator can interpolate and animate map annotation view.
// Instantiate Location Animator
let locationAnimator = DefaultLocationAnimator()// Subscribe for `Location Animator` position updates
locationAnimator.subscribeForFrequentlyUpdatingPosition { position in
// Update map view annotation position here
}// Additionally you can subscribe for infrequently position updates
locationAnimator.subscribeForInfrequentlyUpdatingPosition { position in
// Update map camera position
}var locationUpdateIntervalInMilliseconds: Double
func subscriber(sender: Subscriber, didUpdateDesiredInterval interval: Double) {
locationUpdateIntervalInMilliseconds = interval
}// Feed animator with location changes from the `Subscriber SDK`
func subscriber(sender: Subscriber, didUpdateEnhancedLocation locationUpdate: LocationUpdate) {
locationAnimator.animateLocationUpdate(location: locationUpdate, expectedIntervalBetweenLocationUpdatesInMilliseconds: locationUpdateIntervalInMilliseconds)
}Resources
Feature Support
iOS Client Library feature support matrix.
Support, feedback and troubleshooting
Please visit http://support.ably.com/ for access to our knowledgebase and to ask for any assistance.
You can also view the community reported Github issues.
To see what has changed in recent versions, see the CHANGELOG.
Known Limitations
These SDKs support iOS and iPadOS. Support for macOS / tvOS may be developed in the future, depending on interest / demand.
Contributing
For guidance on how to contribute to this project, see CONTRIBUTING.md.
Package Metadata
Repository: ably/ably-asset-tracking-swift
Stars: 10
Forks: 5
Open issues: 92
Default branch: main
Primary language: swift
License: Apache-2.0
Topics: asset-tracking, cocoa, ios, realtime, sdk, swift
README: README.md
Archived: yes