从App将GPX文件导出到Strava-Swift

时间:2019-03-27 19:57:14

标签: swift export strava

我正在尝试通过在应用程序中创建的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)
        }
    }

0 个答案:

没有答案