Swiftpack.co - Package - FitnessKit/FitDataProtocol


Swift4 Version License Platform

Swift Version of the Garmin Flexible and Interoperable Data Transfer Protocol.

Supports Revision 2.3


FitDataProtocol is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'FitDataProtocol'

Swift Package Manager:

    dependencies: [
        .Package(url: "https://github.com/FitnessKit/FitDataProtocol", from: "0.21.1")

How to Use

Decoding FIT Files

let fileUrl = URL(fileURLWithPath: "WeightScaleMultiUser" + ".fit")
let fileData = try? Data(contentsOf: fileUrl)

if let fileData = fileData {
    var decoder = FitFileDecoder(crcCheckingStrategy: .throws)

    do {

        try decoder.decode(data: fileData,
                       messages: FitFileDecoder.defaultMessages,
            decoded: { (message: FitMessage) in

                print("Got Message: \(message)")

                if message is FileIdMessage {
                    let message = message as! FileIdMessage
                    print("mssage", message.deviceSerialNumber)

                if message is RecordMessage {
                    let message = message as! RecordMessage

                if message is SportMessage {
                    let message = message as! SportMessage

                if message is ActivityMessage {
                    let message = message as! ActivityMessage

    } catch {

Encoding FIT Files

As part of the Encoding of the FIT Files you can check for Validity of the data you are encoding.

The options are

  • none - No Validity Checks are done
  • fileType - Based on the File Type, checks will be done to insure correct fields are included
  • garminConnect - Special Check for creating FIT files for GarminConnect


let time = FitTime(date: Date())

let activity = ActivityMessage(timeStamp: time,
                               totalTimerTime: nil,
                               localTimeStamp: nil,
                               numberOfSessions: nil,
                               activity: Activity.multisport,
                               event: nil,
                               eventType: nil,
                               eventGroup: nil)

let fieldId = FileIdMessage(deviceSerialNumber: nil,
                            fileCreationDate: time,
                            manufacturer: Manufacturer.garmin,
                            product: nil,
                            fileNumber: nil,
                            fileType: FileType.activity,
                            productName: nil)

do {
    let encoder = FitFileEncoder(dataValidityStrategy: .none)

    let data = try encoder.encode(fildIdMessage: fieldId, messages: [activity])
    print(data as NSData)

    /// you can save off the file data

} catch  {


This package is developed and maintained by Kevin A. Hoogheem


FitDataProtocol is available under the MIT license


Stars: 7
Help us keep the lights on


Used By

Total: 0