Mobile SDKs

SpikeSDK iOS (v3)

Current Swift SDK Version: 4.2.21

Swift Package can be found here (api reference)

Requirements

iOS 13.0+

iOS Setup Guide

iOS Signing & Capabilities

To add HealthKit support to your application's Capabilities.

  • Open the iOS/ folder of your project in Xcode
  • Select the project name in the left sidebar
  • Open Signing & Capabilities section
  • In the main view select '+ Capability' and double click HealthKit

More details you can find here.

Info.plist

Add Health Kit permissions descriptions to your Info.plist file.

Text


SDK instalation



CocoaPods

CocoaPods is a dependency manager for Cocoa projects. To integrate SpikeSDK into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'SpikeSDK'

Use pod install and pod update commands to install/update pods afterward.

Swift Package Manager

To integrate SpikeSDK into your Xcode project using Swift Package Manager, add it in your Package.swift or through the Project's Package Dependencies tab:

Text


Spike SDK Usage

Start getting Spike data in 3 steps. All Spike SDK async method calls should be wrapped into try catch block.

Step 1 - Create Spike connection

To set up the Spike SDK create SpikeConnectionV3 instance with your Spike application id, auth token and user id unique to each of your users:

Swift


Step 2 - Ask user for permissions

Provide permissions to access iOS HealthKit data. Spike SDK method will check required permissions and request them if needed. Permission dialog may not be shown according on iOS permissions rules.

Swift


Step 3 - Get data

Info: The maximum permitted date range is 90 days There are two types of data you can retrieve from Spike:

  • Records consist of the raw data points collected from user devices or applications.
  • Statistics, on the other hand, are calculated values derived from records.

Statistics

Get daily statistics for steps and total distance from Apple Health:

Swift


where:

Swift


Records

Swift


where:

Swift


Background delivery

Background delivery ensures that data updates are sent to your backend via webhooks, even when the application is in the background or closed.

Important notes about background delivery on iOS

  • For most data types the most possible frequency of updates is 1 hour.
  • iOS can update data more frequently for some data types, for example, vo2 max.
  • iOS may throttle the frequency of updates for background delivery depending on the app’s activity, battery state, etc.
  • Background delivery is not possible while device is locked, so it will be executed only when the device is unlocked.
  • iOS may stop background delivery if it detects that the app is not active for a long time.
  • The feature is available starting with iOS 15.

Important: The SpikeSDK, along with any other HealthKit applications, cannot guarantee data synchronization on a fixed schedule. The hourly sync interval serves as a guideline rather than a strict requirement enforced by iOS. Consequently, the actual synchronization frequency may vary, occurring hourly, once per day, or during specific system-defined events, such as the conclusion of Sleep Mode or when the device begins charging.

Setup

Enable background delivery for app target

  • Open the folder of your project in Xcode
  • Select the project name in the left sidebar
  • Open Signing & Capabilities section
  • Select HealthKit background delivery under HealthKit section

Initialization at app startup

Add Spike initialization code to your AppDelegate inside application:didFinishLaunchingWithOptions: method:

Swift


For SwiftUI based apps follow few steps:

Create a custom class that inherits from NSObject and conforms to the UIApplicationDelegate protocol:

Swift


And now in your App struct, use the UIApplicationDelegateAdaptor property wrapper to tell SwiftUI it should use your AppDelegate class for the application delegate:

Swift


Ask SpikeSDK for background delivery

To enable background delivery, you need to call the enableBackgroundDelivery method:

Swift


Calling enableBackgroundDelivery will overwrite all the previous settings, so you have to call it with all the data types that you want in one call. It accepts all the possible data types that can be delivered in the background (statistics, records, activities and sleep).

Swift


You can also call getBacgroundDeliveryConfig() to get the current configuration and disableBacgroundDelivery() to disable background delivery.

Reading logs

If you want to receive logs from SpikeSDK, you can use the following code:

Swift


If you are using background delivery, this code should be run in your application’s didFinishLaunchingWithOptions method before you start using SpikeSDK:

Swift