Install SDK on iOS
Requirements
- iOS 11+
- CPU architectures:
arm64
,arm64e
- Simulator architectures:
arm64
,x86_64
(The SDK doesn't do tracking on simulators, but can compile) - Devices with GPS and Motion Co-Processor (excludes iPod Touch and some iPads)
Basic integration
Add HyperTrack SDK to your project
Swift Package Manager
Select File
> Swift Packages
> Add Package Dependency...
. Enter this URL https://github.com/hypertrack/sdk-ios
in the search field to install the dependency.
Swift Package Manager is supported in Xcode 12 and up.
CocoaPods
If you don't have CocoaPods installed, you can install it here.
Using command line run pod init
in your project directory to create a Podfile. Put the following code (changing target placeholder to your target name) in the Podfile:
platform :ios, '11.0'
inhibit_all_warnings!
target 'YourApp' do
use_frameworks!
pod 'HyperTrack', '<version>'
end
Run pod install
. CocoaPods will build the dependencies and create a workspace (.xcworkspace
) for you.
We constantly work on making our SDKs better, so make sure you have the latest version. You can get it in the changelog here.
Enable Background Modes
Enable Background Modes in your project target's Capabilities tab. Choose "Location updates" and "Remote notifications".
In the same tab, ensure that push notifications is enabled.
Add purpose strings
Set the following purpose strings in the Info.plist
file:
The key names are as follows:
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription
NSMotionUsageDescription
Purpose string should explain the value that location and motion tracking provides. Examples of motion tracking benefits: improves battery life by using algorithms based on motion tracking data, provides story-like details for historical tracking data, gives live feedback on current activity.
HyperTrack SDK requires "Always" permissions to reliably track user's location. Be advised, purpose strings are mandatory.
Your app needs to make sure that it has location and motion permissions for location tracking to work. See this F.A.Q. page for details on permissions best practices.
Set up silent push notifications
We use silent push notifications to wake up the app when work is assigned to a driver or to prevent tracking interruptions.
This guide assumes you have configured APNs in your application. If you haven't, read the iOS documentation on APNs.
Configure APNs on the dashboard
Log into the HyperTrack dashboard, and open the setup page. Upload your Auth Key (file in the format AuthKey_KEYID.p8
) and fill in your Team ID.
Initialize the SDK
Get your publishable key from the Setup page.
Put the initialization call inside your AppDelegate
's application:didFinishLaunchingWithOptions:
method:
let publishableKey = HyperTrack.PublishableKey("PASTE_YOUR_PUBLISHABLE_KEY_HERE")!
switch HyperTrack.makeSDK(publishableKey: publishableKey) {
case let .success(hyperTrack):
// Use `hyperTrack` instance
case let .failure(fatalError):
// Handle errors, for example using switch
}
Start tracking
Now the app is ready to be tracked from the cloud. HyperTrack gives you powerful APIs to control device tracking from your backend.
To use the HyperTrack API, you will need the
{AccountId}
and{SecretKey}
from the Setup page.
Track devices during work
Track devices when user is logged in to work, or during work hours by calling the Devices API.
To start, call the start API.
curl -X POST \
-u {AccountId}:{SecretKey} \
https://v3.api.hypertrack.com/devices/{device_id}/start
Get the tracking status of the device by calling GET /devices/{device_id} api.
curl \
-u {AccountId}:{SecretKey} \
https://v3.api.hypertrack.com/devices/{device_id}
To see the device on a map, open the returned embed_url in your browser (no login required, so you can add embed these views directly to you web app). The device will also show up in the device list in the HyperTrack dashboard.
To stop tracking, call the stop API.
curl -X POST \
-u {AccountId}:{SecretKey} \
https://v3.api.hypertrack.com/devices/{device_id}/stop
Track routes with ETA
If you want to track a device on its way to a destination, call the Routes API and add destination.
HyperTrack Trips API offers extra fields to get additional intelligence over the Devices API.
- set destination to track route and ETA
- set scheduled_at to track delays
- share live tracking URL of the trip with customers
- embed live tracking view of the trip in your ops dashboard
curl -u {AccountId}:{SecretKey} --location --request POST 'https://v3.api.hypertrack.com/trips/' \
--header 'Content-Type: application/json' \
--data-raw '{
"device_id": "{device_id}",
"destination": {
"geometry": {
"type": "Point",
"coordinates": [{longitude}, {latitude}]
}
}
}'
To get {longitude}
and {latitude}
of your destination, you can use for example Google Maps.
HyperTrack uses GeoJSON. Please make sure you follow the correct ordering of longitude and latitude.
The returned JSON includes the embed_url for your dashboard and share_url for your customers.
When you are done tracking this trip, call complete Trip API using the trip_id
from the create trip call above.
curl -X POST \
-u {AccountId}:{SecretKey} \
https://v3.api.hypertrack.com/trips/{trip_id}/complete
After the trip is completed, use the Trips API to retrieve a full summary of the trip. The summary contains the polyline of the trip, distance, duration and markers of the trip.
curl -X POST \
-u {AccountId}:{SecretKey} \
https://v3.api.hypertrack.com/trips/{trip_id}
Dashboard
Once your app is running, go to the dashboard where you can see a list of all your devices and their live location with ongoing activity on the map.
Prepare for App Store submission
App Privacy
HyperTrack values user privacy and is not engaging in tracking the user across apps. All the data that the HyperTrack SDK collects is used only to power HyperTrack features. The data is collected on device, and then securely transferred to HyperTrack cloud.
Submitting the App Privacy report
In the App Store Connect select your app and go to App Store > App Privacy. If this is the first time you are submitting a report tap the "Get Started" button; if you already have an existing report tap the "Edit" button next to the "Data Types" section.
- Data Collection. Choose "Yes, we collect data from this app" and tap "Next"
- Scroll to the "Health & Fitness" section and choose "Fitness". HyperTrack SDK uses the user's current activity (walk, drive and stop) to adjust the frequency of collecting location data and conserve battery life by not sending data when the user is not on the move
- Scroll to "Location" section and choose "Precise Location". This is the primary data that the SDK collects and securely transfers to HyperTrack cloud to power all the features
- Scroll to "Identifiers". Choose "User ID". HyperTrack SDK generates a random
device_id
when the SDK is unlocked with the publishable key for the first time. This ID is completely random and does not encode any user-specific information. It is used to identify the user's device in our APIs and it is reset when a user reinstalls the app - Choose "Device ID". HyperTrack SDK uses Identifier for Vendor (IDFV) to identify the user between app reinstalls if the user has multiple apps from you. If the user only has one app from you, this identifier is also reset when a user reinstalls the app. This identifier is available as
os_hardware_identifier
in our APIs - Tap "Publish"
Setting up how the data is collected
If this is the first time you are submitting a report tap "Set Up ..." next to a data identifier. If you already have an existing report tap the "Edit" button next to the specific data identifier.
Fitness
- Choose "App Functionality" and tap "Next"
- Select "Yes, fitness data collected from this app is linked to the user’s identity" and tap "Next" until you reach the next question. This data is linked to the user through the randomly generated "device_id"
- Select "No, we do not use fitness data for tracking purposes" and tap "Publish"
Precise Location
- Choose "App Functionality" and tap "Next"
- Select "Yes, precise location data collected from this app is linked to the user’s identity" and tap "Next". This data is linked to the user through the randomly generated "device_id"
- Select "No, we do not use precise location data for tracking purposes" and tap "Publish"
User ID
- Choose "App Functionality" and tap "Next"
- Select "Yes, user IDs collected from this app are linked to the user’s identity" and tap "Next". In this case the "device_id" is used to identify the user
- Select "No, we do not use user IDs for tracking purposes" and tap "Publish"
Device ID
- Choose "App Functionality" and tap "Next"
- Select "Yes, device IDs collected from this app are linked to the user’s identity" and tap "Next". This data is linked to the user through the randomly generated "device_id"
- Select "No, we do not use device IDs for tracking purposes" and tap "Publish"
Complying with App Store Review Guidelines
HyperTrack SDK requires the use of the UIBackgroundModes
key inside the Info.plist
file to collect the location data when the user is on the move and the app is not on-screen. This is the typical automating work on the move use case. If the App Store review team is not convinced that background location access is justified for your app, they may reject it for violating the 2.5.4 section of App Store Review Guidelines.
Here are some tips that will help you avoid this:
- Your app should have permission strings that clearly explain to your worker why the app needs their location. Those strings will be displayed in permission dialogs and in Settings.app.
- Don't ask for permissions right when the app starts. Instead ask for permissions after the user action that starts location tracking session. It is best to have a separate screen in the app, explaining the use of location tracking with a button that triggers permission dialog.
- App Store review requires your app to have visible features that depend on background location access. For example, your app can have a map, showing the worker his route to a customer, or a visual element explicitly explaining that location tracking is currently active and is used to automatically complete a task the worker is performing.
- When submitting your app for review, include a video showcasing all your app features that require location permissions to test. App Store review team usually doesn't want to grant those permissions and the video significantly speed ups the review process. A link to an unlisted YouTube video works great in most cases.
Recommended additional steps
Identify devices
All devices tracked on HyperTrack are uniquely identified using UUID. You can get this identifier programmatically in your app by calling deviceID
after initialization.
Another approach to identification is to tag devices with names that will make it easy to distinguish them on HyperTrack Dashboard.
hyperTrack.setDeviceName("Device name")
You can additionaly tag devices with custom metadata (and filter them in the Dashboard using metadata fields). Metadata should be representable in JSON.
hyperTrack.metadata = ["key": "value"]
Handle errors
Use the errors
query or subscription to make sure that when the driver navigates to the screen where tracking is supposed to happen, there are no blockers that can prevent that.
You can use subscription API to be able to react immediately when errors come up:
// The lifetime of this value is linked to the lifetime of subscription.
// Usually is declared as a property of a ViewController.
var errorsCancellable: HyperTrack.Cancellable? = nil
errorsCancellable = hyperTrack.subscribeToErrors { errorsSet in
for error in errorsSet {
switch error {
case .locationPermissionsDenied:
// Handle case when location permissions are denied
case ...
}
}
}
Or by querying the API only when needed:
for error in hyperTrack.errors {
switch error {
case .locationPermissionsDenied:
// Handle case when location permissions are denied
case ...
}
}
Reference
For a full SDK API reference see HyperTrack iOS SDK Reference
SDK integration examples
To learn more about SDK integration examples, you may visit these resources:
Support
Join our Slack community for instant responses. You can also email us at help@hypertrack.com.
Frequently Asked Questions
What iOS versions are supported?
Currently we do support all of the iOS versions starting from iOS 11.
Why doesn't my iOS app start tracking in the background state?
Make sure your app has location and motion permissions to start tracking.
Get permissions before your app is in the background state
For example, if your app does the following steps:
- User logs in
- Starts a shift
- App calls your app backend which calls HyperTrack API to create a trip
then at this point the app can already be in the background because the user has already switched to other tasks. HyperTrack cannot start tracking on the device for the trip as permissions cannot be requested while the app is in the background state.
If location and motion permissions are not granted in the foreground prior to the app going to the background state, HyperTrack SDK cannot request them to start location tracking in your app in the background state.
That’s why it’s important to make sure that the app has all required permissions before the user can start a shift.
To make this happen, your app needs to use CLLocationManager
and CMMotionActivityManager
to request authorization.
Usually apps detect current permissions state first and, if they are not granted, show a special screen allowing the user to grant them right there, or if they were denied to switch them on in Settings.app
.
This way, when the user starts a new shift, push notification would reach the app, the app would have all the necessary permissions to track.
Why did my iOS app stop tracking?
OS regularly drops application memory when it needs more memory for the app currently in foreground.
Test memory usage on device
Quickest way to check this is to run a game or open a Camera app and record a video. The OS sorts all apps frozen in the background by memory footprint and starts dropping memory one by one starting with the largest ones.
When iOS does this, it creates a file called "JetsamEvent", which you can see in Settings
> Privacy
> Analytics & Improvements
> Analytics Data
.
For more details see this Apple Developer Note.
This is typical iOS behavior and no app can assume that the OS will always keep memory for frozen apps.
iOS versions 13.2 and 13.2.1 had a bug which caused the scheduler to drop memory for the frozen apps more aggressively.
Also, your app should not rely on memory to be preserved and should store and then restore their state when the user launches the app.
Sometimes application bugs, like memory leaks, can cause the app to consume a lot of RAM, and become one of the first apps in the iOS list for the memory reclaim process.
Prevent excessive memory usage on iOS device
To prevent this, your app should be regularly profiled in Xcode using Instruments tool, configured with Leaks
preset. It provides two views into the app memory called Allocations and Leaks.
Allocations allow you to see how much RAM your app is consuming and how this size grows and shrinks in response to user actions.
Leaks
tool automatically reports when memory is created and would never be freed. HyperTrack SDK profiled in Instruments consumes under 1 MB of RAM and doesn't have memory leaks.
Good starting point can be Instruments documentation by Apple. iOS also sends low memory warnings to the app when it detects that it consumes too much.
It's also useful to checkout Apple guide on improving your app's performance.
If you’ll find any instances of HyperTrack SDK consuming too much RAM, or being a reason for abrupt terminations, please do not hesitate to contact us and provide crash reports or Instrument
analysis files. Instruments
sessions can be saved and shared.
Prepare your app to run in the background
iOS can also abruptly terminate the app upon entering the background. This can happen if, upon entering background, the app does not free resources and does not stop long running tasks, like timers and network requests.
The app without a reason to stay in the background has only a couple of seconds to stop all its activities and if it fails, the OS will force the termination.
The following guide by Apple provides all the steps the app needs to do upon entering background and can serve as a checklist.
Why does my application state disappear when my app is opened?
App state is not preserved in the background
App does not hold state when it goes to the background state. When the user returns to the app, it starts from scratch. iOS stops executing an app and freezes its memory when the user hides the app's screen.
The only exceptions are apps that actively do background work, such as:
- downloading content
- VoIP calls
- active location tracking
There are three things that can happen to the app in a stopped and frozen state:
- The app's memory is stored in RAM and when the user returns to the app execution continues where he left off
- OS drops the app's memory, so when the user returns, the app needs to restore state manually
- OS terminates app execution abruptly, crashing the app in the background
This is typical iOS behavior and no app can assume that the OS will always keep memory for frozen apps.
iOS versions 13.2 and 13.2.1 had a bug which caused the scheduler to drop memory for the frozen apps more aggressively.
Apps shouldn't rely on memory to be preserved and should store and then restore their state when the user launches the app.
A good starting point to learn how to manage restoring app state is newly updated UI state restoration guide.
What are the best practices for handling permissions on iOS?
In Human Interface Guidelines Apple recommends the following instructions below:
Request permissions only when they are needed in the flow of the app
If you app is centered around location tracking, then asking for permissions at the app launch can be understandable for users. On the other hand, if location tracking is just one of the features, then it makes sense to request them only when the feature is activated.
Provide short and specific purpose string
Purpose string should explain the value that location and motion tracking provides. Examples of motion tracking benefits: improves battery life by using algorithms based on motion tracking data, provides story-like details for historical tracking data, gives live feedback on current activity.
In addition a lot of great apps provide a special screen explaining the need for permissions before asking them. If permissions are denied you can guide the user to the specific page in the Settings.app to change permissions (see this guide for special deep-links for the Settings.app).
"Provisional Always" authorization state
On iOS 13 Apple introduced a new "Provisional Always" authorization state (see this StackOverflow answer for details).
In short:
- there is no API to detect this state
- during this state there are no location events in background
- user sees his permissions as granted and sees "While Using" state in Settings.app
- app sees permissions as granted with "Always" state.
HyperTrack is working on ways to detect this state and provide APIs that would enable app developers to display explanation screens that will guide the user back to Settings.app to switch permissions from "While Using" to "Always".
Why Access to Activity services has not been authorized?
You are running the Quickstart app on the iOS simulator, which currently does not support CoreMotion services.
You can test the app on real iOS devices only.