Swiftpack.co -  FitnessKit/TcxDataProtocol as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Training Center XML Format
.package(url: "https://github.com/FitnessKit/TcxDataProtocol.git", from: "1.0.1")


Swift5 Version License Platform Readme Score

Provides a Swift version of the TCX XML format.


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

pod 'TcxDataProtocol'

Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/FitnessKit/TcxDataProtocol", from: "1.0.0")

How to Use


let tcxUrl = URL(fileURLWithPath: "TestFile" + ".tcx")
let tcxData = try? Data(contentsOf: tcxUrl)

if let tcxData = tcxData {
    let tcxFile = try? TcxFile.decode(from: tcxData)


Each Trackpoint time has to be unique. Below example shows a per second increment from the previous.

let workoutStartDateTime = Date()
let build = Build(version: Version(major: 0, minor: 1, buildMajor: 0, buildMinor: 0), time: nil, builder: nil, type: .alpha)
let author = Author(name: "TcxDataProtocol", build: build, language: nil, partNumber: "11-22-33")
let track = [Track(trackPoint: [
                    Trackpoint(time: workoutStartDateTime.addingTimeInterval(Double(1)), position: nil, altitude: nil, distance: 1.11, heartRate: HeartRateInBeatsPerMinute(heartRate: 100), cadence: 100, sensorState: SensorState.present, extensions: [Extension(activityTrackpointExtension: ActivityTrackpointExtension.init(speed: 11.2, runCadence: nil, watts: 111, cadenceSensor: CadenceSensorType(rawValue: "bike")), activityLapExtension: nil, activityGoals: nil)]),
                    Trackpoint(time: workoutStartDateTime.addingTimeInterval(Double(2)), position: nil, altitude: nil, distance: 1.11, heartRate: HeartRateInBeatsPerMinute(heartRate: 101), cadence: 101, sensorState: SensorState.present, extensions: [Extension(activityTrackpointExtension: ActivityTrackpointExtension.init(speed: 11.3, runCadence: nil, watts: 222, cadenceSensor: CadenceSensorType(rawValue: "bike")), activityLapExtension: nil, activityGoals: nil)])

let lap = ActivityLap(startTime: workoutStartDateTime, totalTime: 45.0, distance: 12.0, maximumSpeed: nil, calories: 120, averageHeartRate: nil, maximumHeartRate: nil, intensity: .active, cadence: nil, triggerMethod: .manual, track: track, notes: nil, extensions: nil)

let activity = Activity(sport: .biking, identification: workoutStartDateTime, lap: [lap], notes: nil, training: nil, creator: nil)

let activities = ActivityList(activities: [activity], multiSportSession: nil)

let database = TrainingCenterDatabase(activities: activities, courses: nil, author: author)

let TCXFile = TcxFile(database: database)

let encodedData = try? TCXFile.encode(prettyPrinted: true)

if let encodedData = encodedData {
    let xml = String(bytes: encodedData, encoding: .utf8)

Supported Elements

  • ActivityList
  • Activity
  • ActivityLap
  • ActivityReference
  • Application
  • Author
  • Build
  • BuildType
  • Course
  • CourseLap
  • CourseList
  • CoursePoint
  • CoursePointType
  • Creator
  • Gender
  • HeartRateInBeatsPerMinute
  • HeartRateInBeatsPercentMax
  • Intensity
  • MultiSportSession
  • NextSport
  • Plan
  • Position
  • QuickWorkoutResults
  • SensorState
  • Sport
  • Track
  • Trackpoint
  • Training
  • TrainingType
  • TriggerMethod
  • Version

Supported Extensions

  • ActivityExtension

    • CadenceSensorType
    • ActivityTrackpointExtension
    • ActivityLapExtension
  • ActivityGoals

    • Measure
    • RecurrenceCode
    • Period
    • ActivityGoal
    • ActivityGoals


This package is developed and maintained by Kevin A. Hoogheem


TcxDataProtocol is available under the MIT license


Stars: 8
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

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