[英]Dropbox for Swift: Upload a csv file to Dropbox using SwiftyDropbox(cocoapods) not working
我创建了一个创建 CSV 文件的小应用程序,现在我想将它上传到 Dropbox 文件夹。
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
}
我应该怎么做才能直接写入应用程序保管箱文件夹?
谢谢!
这是我用于将 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”(文件上传到我在 App Console 中指定的目录下的 Apps 目录中的保管箱)。 因此,workout.tcx 文件将被写入
-- dropbox
-- Apps
-- Breakaway
-- workout.tcx
latestTCXpath 是在 iOS 应用程序的文档目录中存储锻炼.tcx 的完整路径名。 模拟器中的示例:
file:///Users/testUser/Library/Developer/CoreSimulator/Devices/B4759D88-DFB6-43C0-A6E9-84FE3F23B9A0/data/Containers/Data/Application/8C348B27-FB2E-42E2-8425-92844DF18596/Documents/workout.tcx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.