1

我正在尝试通过在应用程序中创建的 gpx 文件将我的健身信息从我的应用程序发送到 strava(该文件被创建并填充没有任何问题)。我做了一些研究,但似乎找不到正确的答案,我发现的一切都是关于电子邮件、消息甚至空投的。当我运行我的代码时,它会返回我的文件,但没有关于将文件上传到 strava。控制台也返回了这个错误:

(String) = "文件“”不存在。" 迅速

这是我到目前为止所做的:

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)
        }
    }
4

0 回答 0