Swiftpack.co - FitnessKit/FitDataProtocol as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by FitnessKit.
FitnessKit/FitDataProtocol 2.1.6
Swift Implementation the Garmin Flexible and Interoperable Data Transfer Protocol.
⭐️ 40
🕓 1 week ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/FitnessKit/FitDataProtocol.git", from: "2.1.6")


Swift5.2 Version License Platform

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

Supports SDK Revision 21.16.0


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: "2.1.4")

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 let message = message as? FileIdMessage {
                print("mssage", message.deviceSerialNumber)

            if let message = message as? RecordMessage {

            if let message = message as? SportMessage {

            if 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 activity = ActivityMessage(timeStamp: FitTime(date: Date()),
                               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)

let encoder = FitFileEncoder(dataValidityStrategy: .none)

let result = encoder.encode(fildIdMessage: fiel, messages: [activity])
switch result {
case .success(let encodedData):
    print(encodedData as NSData)
    /// you can save off the file data
case .failure(let error):

/// You can still use doCatch 
do {
    let encoder = FitFileEncoder(dataValidityStrategy: .none)

    let data = try encoder.encode(fildIdMessage: fieldId, messages: [activity]).get()
    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: 40
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics