I like to upload my workouts to Strava. I usually track workouts using the WorkOutDoors AW app, which offers an “Upload to Strava” button after ending a workout. It's simple and works well. This is low priority, but it would be nice to have someday.
Strava has a nice API, easy to use:
  1. Authenticate via oAuth flow to get a persistant token (just once).
  2. Upload workouts using the Upload Activity API endpoint.
Here's instructions from ChatGPT for implementing this in Swift:
To upload a workout with timeseries heart rate data to Strava, you'll need to create a file in a supported format (e.g., .FIT, .TCX, or .GPX) that includes the heart rate data. Here, we'll focus on creating a .TCX (Training Center XML) file, as it's a common format for such data. After creating the file, you'll use the Strava API's "Upload Activity" endpoint to upload this file.
### Step 1: Create a .TCX File
You need to create a .TCX file that includes your timeseries heart rate data. A simplified .TCX example with heart rate data might look like this:
<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="WeightTraining">
<Id>2024-02-21T09:00:00Z</Id>
<Lap StartTime="2024-02-21T09:00:00Z">
<TotalTimeSeconds>3600</TotalTimeSeconds>
<Track>
<Trackpoint>
<Time>2024-02-21T09:00:00Z</Time>
<HeartRateBpm>
<Value>120</Value>
</HeartRateBpm>
</Trackpoint>
<!-- Add more Trackpoint entries here with respective times and heart rates -->
</Track>
</Lap>
</Activity>
</Activities>
</TrainingCenterDatabase>
### Step 2: Upload the .TCX File Using Strava API
After creating your .TCX file, you'll use the Strava API's "Upload Activity" endpoint to upload the file. Here's how you can do it in Swift, using
URLSession
for the network request:
import Foundation
let token = "YOUR_ACCESS_TOKEN" // Replace with your actual access token
let url = URL(string: "https://www.strava.com/api/v3/uploads")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
let fileURL = URL(fileURLWithPath: "path/to/your/file.tcx") // Path to your .TCX file
let fileName = "workout.tcx"
var body = Data()
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"file\"; filename=\"\(fileName)\"\r\n".data(using: .utf8)!)
body.append("Content-Type: application/octet-stream\r\n\r\n".data(using: .utf8)!)
body.append(try! Data(contentsOf: fileURL))
body.append("\r\n".data(using: .utf8)!)
body.append("--\(boundary)--\r\n".data(using: .utf8)!)
request.httpBody = body
let session = URLSession.shared
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
print("Error: \(error)")
return
}
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("Response data: \(dataString)")
}
}
task.resume()
This Swift code constructs a multipart/form-data request to upload your .TCX file to Strava. Make sure to replace
"YOUR_ACCESS_TOKEN"
with your actual access token and
"path/to/your/file.tcx"
with the path to your .TCX file.
After uploading, Strava processes the file and creates the activity, including the timeseries heart rate data.