简体   繁体   English

可以将 iOS 应用程序文档目录托管为 FTP 服务器的库

[英]Library that can host iOS app document directory as FTP server

I have an iOS app with a specific purpose, it creates some files, and allows the user to retrieve these files.我有一个具有特定用途的 iOS 应用程序,它创建了一些文件,并允许用户检索这些文件。 I have already implemented iTunes file sharing and AirDrop.我已经实现了 iTunes 文件共享和 AirDrop。 But sometimes, it is easier to just host all the files as an FTP server so that it is a lot easier to just copy multiple files off into computer.但有时,将所有文件作为 FTP 服务器托管会更容易,因此将多个文件复制到计算机中会容易得多。 However, I haven't find any library in iOS that can host files as FTP Server.但是,我还没有在 iOS 中找到任何可以将文件托管为 FTP 服务器的库。 I did find DFServer on GitHub.我确实在 GitHub 上找到了DFServer But it says it is used for debugging only (and when I actually tried it it isn't too stable either).但它说它仅用于调试(当我实际尝试时它也不太稳定)。

What is a good FTP server API for iOS that I can use to host some files (they are all in the document directory of my app)?什么是适用于 iOS 的好的 FTP 服务器 API,我可以使用它来托管一些文件(它们都在我的应用程序的文档目录中)? Even better if it supports writing to the directory as well.如果它也支持写入目录,那就更好了。

I use urlComponents to access my ftp server.我使用urlComponents来访问我的 ftp 服务器。 Here is an example of getting the files and saving them in an array:这是获取文件并将它们保存在数组中的示例:

func getFiles(completionHandler: @escaping (Int) -> ()) {
    var urlComponents = URLComponents(string: nameOfFTPServer) // i.e. ftp://me.myserver.com
    urlComponents?.user = "username"
    urlComponents?.password = "password"
    let url = urlComponents?.url
    let request = URLRequest(url: url!)
    var myError = 0

    if url != nil {
        let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
            //no internet connection
            if data == nil || (data?.isEmpty)! {
                myError = NSURLErrorResourceUnavailable
                completionHandler(myError)
            }
            // If error is nil, everything's all good so get the data
            else if error == nil {
                if let data = data, let result = String(data: data, encoding: String.Encoding.ascii) {
                    let array = result.components(separatedBy: "\n")
                    completionHandler(myError)
                }
            }
            else {
                // everything is not all good, handle the error elsewhere
                print(myError!)
                myError = NSURLErrorTimedOut
                completionHandler(myError)
            }
        })

        task.resume()
    }

and my completion handler in another class:和我在另一个班级的完成处理程序:

getFTPFiles(completionHandler: { done in
    DispatchQueue.main.async {
        switch done {
        case 0: // data loaded
        case NSURLErrorResourceUnavailable, NSURLErrorTimedOut: // something went wrong
        default:
            self.tableView.reloadData()
        }
    }
}

This gets me an array of filenames that I parse out later to display in a tableView, and then to open a file (I'm opening a PDF into a webView here):这为我提供了一组文件名,我稍后将其解析以显示在 tableView 中,然后打开一个文件(我在此处将 PDF 打开到 webView 中):

func loadPDF(filename: String, folder: String){
    var urlComponents = URLComponents(string: nameOfServer)
    urlComponents?.user = "username"
    urlComponents?.password = "password"
    urlComponents?.path = "/" + folder + "/" + filename
    let url = urlComponents?.url
    if(url != nil){
        let requestObj = URLRequest(url: url!)
        webView.loadRequest(requestObj)
    }
    else{
        webView.loadHTMLString("<html><body><p>Check your internet connection</p></body></html>", baseURL: nil)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将iOS应用崩溃报告保存到文档目录吗? - Can I save iOS App Crash Report to Document Directory? 如何使用FTP直接从iOS上传图像到服务器目录 - How can i upload an image to server directory directly form iOS using FTP 从FTP服务器将目录/子目录下载到ios应用程序文件夹 - Downloading a directory/subdirectory from the FTP server to ios application folder 锁定/保护iOS应用程序的文档目录 - Lock/Protect document directory of ios app 将应用程序资源放在iOS中的文档目录中 - Putting App Resources in Document Directory in iOS iOS:如何在上传时检查FTP服务器上是否已存在目录? - iOS:How to check if an directory already exists on the FTP server while uploading? 我可以在iOS应用程序的“文档”或“库”目录中放置和访问静态库(.a)吗? - Can I place and access a static library (.a) in the Documents or Library directory for an iOS app? 使用Library目录中的文件释放IOS应用程序 - Releasing IOS app with files in Library directory 如何从其他iOS应用程序读取应用程序文档目录中的文档? - How can I read documents in my app's Document directory from other iOS apps? 将图像从文档目录复制到 Swift iOS 中的照片库 - Copy image from document directory to photos library in Swift iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM