macOS如何实现FTP支持

问题描述 投票:0回答:1

我需要将文件上传到FTP服务器,检查是否存在文件夹,如果不存在,请创建它。我找到了Amir Abbas Mousavian的FileProvider。我已经安装了所有东西,并实现了以下代码:

let credential = URLCredential(user: "user", password: "password", persistence: .permanent)
let ftpProvider = FTPFileProvider(baseURL: URL(string:"cflm.org")!, mode: .active, credential: credential, cache: nil)
ftpProvider?.delegate = self as! FileProviderDelegate
ftpProvider?.contentsOfDirectory(path: "/", completionHandler: {
    contents, error in
    for file in contents {
        print("Name: \(file.name)")
        print("Size: \(file.size)")
        print("Creation Date: \(file.creationDate)")
        print("Modification Date: \(file.modifiedDate)")
    }
})

当我运行代码时,contentsOfDirectory的completionHandler永远不会触发。有谁知道我在做什么错或有其他方法可以执行所需的FTP功能?

swift macos ftp fileprovider
1个回答
0
投票

因为ftpProvider为零,所以未调用您的闭包,快速查看源代码可以发现发送到init的url需要包含协议。因此,在您的网址中添加“ ftp”或“ ftps”。

并将init包裹在guard语句中

guard let ftpProvider = FTPFileProvider(baseURL: URL(string:"ftp://cflm.org")!, mode: .active, credential: credential, cache: nil) else {
    return //or some error handling
} 
//... rest of code
© www.soinside.com 2019 - 2024. All rights reserved.