Mobile SDKs

SpikeSDK iOS

current swift sdk version 4 2 41 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 https //developer apple com/documentation/healthkit/setting up healthkit info plist add health kit permissions descriptions to your info plist file \<key>nshealthshareusagedescription\</key> \<string>we will use your health information to better track workouts \</string> \<key>nshealthupdateusagedescription\</key> \<string>we will update your health information to better track workouts \</string> 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 dependencies \[ package(url "https //gitlab com/spike api/spike ios sdk", uptonextminor(from "2 3 0")) ] 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 import spikesdk static func createconnectionapiv3( applicationid int, signature string, enduserid string ) async throws > spikeconnectionapiv3 ) 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 try await spikeconnectiontemp requestpermissions(forstatistics \[ stepstotal, distancewalking, ] ) 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 func getstatistics( oftypes types \[statisticstype steps], from date now\ addingtimeinterval( 60 60 24), to date now, interval statisticsinterval hour, filter statisticsfilter(providers \[ apple]) ) async throws > \[statistic] where public enum statisticstype string { case steps case distancetotal = "distance total" case distancewalking = "distance walking" case distancecycling = "distance cycling" case distancerunning = "distance running" case caloriesburnedtotal = "calories burned total" case caloriesburnedbasal = "calories burned basal" case caloriesburnedactive = "calories burned active" } public enum statisticsinterval string, codable { case hour case day } public struct statisticsfilter { public var excludemanual bool = false public var providers \[provider]? = nil } // result public struct statistic codable, hashable { public var start date public var end date public var duration int public var type statisticstype public var value double public var unit unit public var rowcount int? public var recordids \[uuid]? } records func getrecords( oftypes types \[metrictype stepstotal], from date now\ addingtimeinterval( 60 60 24), to date now, filter statisticsfilter(providers \[ apple]) ) async throws > \[record] where public enum metrictype string, codable, hashable { case stepstotal case distancetotal case distancewalking case distancecycling case distancerunning case caloriesburnedactive case caloriesburnedbasal case caloriesburnedtotal } // result public struct record codable, hashable { public var recordid uuid public var inputmethod inputmethod? public var startat date public var endat date? public var modifiedat date public var duration int? public var provider provider? public var providersource providersource? public var issourceaggregated bool? public var source recordsource? public var metrics \[string double]? public var activitytags \[activitytag]? public var activitytype activitytype? public var sessions \[activityentry]? public var laps \[activityentry]? public var segments \[activityentry]? public var splits \[activityentry]? public var samples \[activitysamples]? public var routepoints \[activitysamples]? } 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 import spikesdk func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]?) > bool { spike configure() } for swiftui based apps follow few steps create a custom class that inherits from nsobject and conforms to the uiapplicationdelegate protocol import spikesdk class appdelegate nsobject, uiapplicationdelegate { func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]? = nil) > bool { spike configure() return true } } and now in your app struct, use the uiapplicationdelegateadaptor property wrapper to tell swiftui it should use your appdelegate class for the application delegate \@main struct yourapp app { @uiapplicationdelegateadaptor(appdelegate self) var appdelegate } ask spikesdk for background delivery to enable background delivery, you need to call the enablebackgrounddelivery method do { try await spikeconnection enablebackgrounddelivery( forstatisticstypes \[ steps] ) } catch { print("\\(error)") } 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) func enablebackgrounddelivery( forstatistics \[statisticstype]?, formetrics \[metrictype]?, foractivities \[activityconfig]?, forsleep \[sleepconfig]? ) async throws or public struct backgrounddeliveryconfig codable, hashable, sendable { public var statisticstypes \[statisticstype]? public var metricstypes \[metrictype]? public var activityconfigs \[activityconfig]? public var sleepconfigs \[sleepconfig]? } 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 spike setlogcallback { level, message in print("\\(message)") } if you are using background delivery, this code should be run in your application’s didfinishlaunchingwithoptions method before you start using spikesdk func application( application uiapplication, didfinishlaunchingwithoptions launchoptions \[uiapplication launchoptionskey any]?) > bool { spike setlogcallback { level, message in print("\\(message)") } spike configure() }