1

I'm trying to send my fitness info from my app to strava via a gpx file created in app (the file get created and populated without any problem). I've done some research but can't seem to find the right answer, everything I found is about Email, message or even airdrop. When I run my code it return me with the file but nothing about uploading the file to strava. The console return me this error as well:

(String) = "The file “” doesn't exist." swift

Here's what I've done so far:

func saveWorkout(_ workout: FirebaseWorkout, completion: @escaping (_ success: Bool, _ error: Error?) -> Void) {
        let gpxString = workout.getGpx()

        do {
            let documentsDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            let fileURL = documentsDir.appendingPathComponent("workoutData").appendingPathExtension("gpx")

            print("File Path: \(fileURL.path)")

            try gpxString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
            guard let fileData = try? Data(contentsOf: fileURL) else {
                workoutTT().analytics("EXPORT_GPX_ERROR")
                completion(false, nil)
                return
            }

            let opts: [String: Any?] = [
                "file": fileData,
                "data_type": "gpx",
                "name": workout.name,
                "description": workout.notes
            ]

            if self.isAuthorized && userSettings.membership.hasFullAccess {
                workoutTT().analyticsEvent(kEventCategoryThirdParty, eventAction: "SAVE_WORKOUT", eventLabel: ThirdPartyApplication.strava.rawValue, eventValue: 1)
                workoutTT().debugLog("Strava: Save Workout")
                self.refreshTokenIfNeeded {
                    _ = self.oauthswift.client.post("https://www.strava.com/api/v3/uploads", parameters: opts as OAuthSwift.Parameters, success: { (_) in
                        workoutTT().debugLog("Strava: Save Workout Success")
                        completion(true, nil)
                    }) { (error) in
                        workoutTT().debugLog("Strava: Save Workout \(error.localizedDescription)")
                        completion(false, error)
                    }
                }
            } else {
                completion(false, nil)
            }

        } catch {
            workoutTT().analytics("GPX_CONVERT_CATCH_ERROR", debug: true, timestamp: true, doPrint: true)
            // failed to write file – bad permissions, bad filename, missing permissions, or more likely it can't be converted to the encoding
            completion(false, nil)
        }
    }
Frank
  • 113
  • 10
  • You can take a look at this --> https://stackoverflow.com/q/56161869/14414215 In your code above, the thing that stands out is you're doing contentsOf for fileData but I believe you should be just sending it the path to the GPX file. – app4g Dec 04 '20 at 07:21

0 Answers0