使用IOS 9.1,运行在Xcode 7.1.1上。在埃尔卡皮坦之下。我正在尝试通过他们的(可能是新的)SwiftDropbox 库访问 Dropbox 的教程,请按照此页面进行操作。
开始顺利,设法连接并获得授权,但无法做更多事情,因为发布到他们的教程的代码无法编译。
if let client = Dropbox.authorizedClient {
// Get the current user's account info
client.usersGetCurrentAccount().response { response, error in
if let account = response {
println("Hello \(account.name.givenName)")
} else {
println(error!)
}
}
// List folder
client.filesListFolder(path: "").response { response, error in
if let result = response {
println("Folder contents:")
for entry in result.entries {
println(entry.name)
}
} else {
println(error!)
}
}
抱怨
DropboxClient
没有成员“usersGetCurrentAccount”或“filesListFolder”,我认为它们是某种排序方法调用{对于 swift 来说也是相当新的}。谷歌似乎找不到任何有用的线索?这段代码应该读/说什么?
邦,
谢谢你,smarx;你让我走上了正确的道路。这是最终的代码;对他在这里提到的教程进行了两个小修正。
// ViewController.swift
import UIKit
import SwiftyDropbox
class ViewController: UIViewController {
@IBAction func linkButtonPressed(sender: AnyObject) {
if (Dropbox.authorizedClient == nil) {
Dropbox.authorizeFromController(self)
} else {
print("User is already authorized!")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Verify user is logged into Dropbox
if let client = Dropbox.authorizedClient {
// Get the current user's account info
client.users.getCurrentAccount().response { response, error in
print("*** Get current account ***")
if let account = response {
print("Hello \(account.name.givenName)!")
} else {
print(error!)
}
}
// List folder
client.files.listFolder(path: "").response { response, error in
print("*** List folder ***")
if let result = response {
print("Folder contents:")
for entry in result.entries {
print(entry.name)
}
} else {
print(error!)
}
}
// Upload a file
let fileData = "Hello!".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
client.files.upload(path: "/hello.txt", body: fileData!).response { response, error in
if let metadata = response {
print("*** Upload file ****")
print("Uploaded file name: \(metadata.name)")
print("Uploaded file revision: \(metadata.rev)")
// Get file (or folder) metadata
client.files.getMetadata(path: "/hello.txt").response { response, error in
print("*** Get file metadata ***")
if let metadata = response {
if let file = metadata as? Files.FileMetadata {
print("This is a file with path: \(file.pathLower)")
print("File size: \(file.size)")
} else if let folder = metadata as? Files.FolderMetadata {
print("This is a folder with path: \(folder.pathLower)")
}
} else {
print(error!)
}
}
// Download a file
let destination : (NSURL, NSHTTPURLResponse) -> NSURL = { temporaryURL, response in
let fileManager = NSFileManager.defaultManager()
let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
// generate a unique name for this file in case we've seen it before
let UUID = NSUUID().UUIDString
let pathComponent = "\(UUID)-\(response.suggestedFilename!)"
return directoryURL.URLByAppendingPathComponent(pathComponent)
}
client.files.download(path: "/hello.txt", destination: destination).response { response, error in
//client.files.download(path: "/hello.txt").response { response, error in
if let (metadata, url) = response {
print("*** Download file ***")
let data = NSData(contentsOfURL: url)
print("Downloaded file name: \(metadata.name)")
print("Downloaded file url: \(url)")
print("Downloaded file data: \(data)")
} else {
print(error!)
}
}
} else {
print(error!)
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
您正在阅读 5 月份发布的有关 SDK 早期预览版的博客文章。自那篇文章以来,很多事情都发生了变化!查看 https://www.dropbox.com/developers/documentation/swift#tutorial 获取最新教程。
我相信你想要
users.getCurrentAccount
和files.listFolder
。