Swift macOS 如何实现 FTP 支持

标签 swift macos ftp fileprovider

<分区>

我需要将文件上传到 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 功能吗?

最佳答案

你的闭包没有被调用,因为 ftpProvider 是 nil,快速查看源代码告诉我们发送到 init 的 url 需要包含协议(protocol)。因此,将“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

关于Swift macOS 如何实现 FTP 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58137405/

相关文章:

ios - 在默认的 UITableViewCell 中显示一个 imageView

c - SDL2_mixer 在 mac 上初始化失败(运行时)

c# - 如何在 C# 中使用空密码登录 FTP?

ios - 在没有插入闪电电缆的情况下,如何在真实的 iOS 设备上查看实时控制台日志?

ios - swift 中的雅虎天气 API oauth 请求中的额外参数

cocoa - 在 cocoa 应用程序中进行进程外工作吗?

python - 从 FTP 文件夹下载所有文件时出现 "Permission denied"错误

linux - 将参数传递给 FTP 宏

ios - 错误:“预期返回Int的函数中缺少返回”

c - Mac OS X 中 C 变量的奇怪地址