Swift的Dropbox:使用SwiftyDropbox(cocoapods)无法将CSV文件上传到Dropbox

时间:2018-06-22 17:31:25

标签: ios swift dropbox swiftydropbox

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

Problem Schema

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
                        }

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

谢谢!

1 个答案:

答案 0 :(得分: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的官方文档发现的两件事情。

fullPathName是“ /” +“ workout.tcx”(文件已上传到我在App Console中指定的目录下的Apps目录中的我的保管箱中)。因此,execution.tcx文件将被写入

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

latestTCXpath是execution.tcx在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