[英]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.