0

我创建了一个创建 CSV 文件的小应用程序,现在我想将其上传到保管箱文件夹。

问题模式

import SwiftyDropbox

class ViewController: UIViewController {

    func myButtonInControllerPressed() {
        DropboxClientsManager.authorizeFromController(UIApplication.shared,
                                                      controller: self,
                                                      openURL: { (url: URL) -> Void in
                                                        UIApplication.shared.openURL(url)
        })
    }

let client = DropboxClientsManager.authorizedClient
        let fileData = "teste.csv".data(using: String.Encoding.utf8, allowLossyConversion: false)!

        client?.files.upload(path: "/test/path/in/Dropbox/account/HealthkitFromTheGround", input: fileData)
            .response { response, error in
                if let response = response {
                    print(response)
                } else if let error = error {
                    print(error)
                }
            }
            .progress { progressData in
                print(progressData)
        }

let fileName = "teste.csv"
                let tempDir = NSTemporaryDirectory()
                let fileURL = URL(fileURLWithPath: tempDir, isDirectory: true).appendingPathComponent(fileName)

do {
                            try csvString.write(to: fileURL, atomically: true, encoding: .utf8)
                            print("Created file")
                        } catch {
                            print("Failed to create file: \(error)")
                            return
                        }

我应该怎么做才能直接写入应用程序保管箱文件夹?

谢谢!

4

1 回答 1

0

这是我用于将 TCX 文件(它与您的 csv 文件基本相同,因为它也是基于文本的)上传到 Dropbox(我正在使用保护程序)范围内的文件夹

  static func dropboxSync() {
      let client = DropboxClientsManager.authorizedClient
      let latestTCXpath = TCXfuncs.listTCX()[0]
      let fullPathName =  "/" + latestTCXpath.lastPathComponent
      
      let fileData = try! Data(contentsOf: latestTCXpath)
      
      _ = client?.files.upload(path: fullPathName, input: fileData)
        .response { response, error in
          if let response = response {
            print(response)
          } else if let error = error {
            print(error)
          }
        }
      //        .progress { progressData in
      //          print(progressData)
    }
    

注意:这些是我根据 GitHub 上 SwiftyDropbox 的官方文档感到困惑的 2 个项目。

fullPathName 是“/”+“workout.tcx”(文件上传到我在应用程序控制台中指定的目录下的应用程序目录中的保管箱)。因此,workout.tcx 文件将被写入

 -- dropbox
   -- Apps
     -- Breakaway
       -- workout.tcx

latestTCXpath 是在 iOS 应用程序的文档目录中存储锻炼的完整路径名。模拟器中的示例:

file:///Users/testUser/Library/Developer/CoreSimulator/Devices/B4759D88-DFB6-43C0-A6E9-84FE3F23B9A0/data/Containers/Data/Application/8C348B27-FB2E-42E2-8425-92844DF18596/Documents/workout.tcx
于 2020-12-11T07:22:14.787 回答