I am not able to understand how to download all the files from the dirextory "ABC" ?
import UIKit
class ViewController: UIViewController, NSURLConnectionDataDelegate, UIDocumentInteractionControllerDelegate {
var filePath: String!
var fileStream: NSOutputStream!
var request: NSURLRequest!
var connection: NSURLConnection!
var data: NSMutableData = NSMutableData()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func startDownload(sender: AnyObject) {
// here ABC is the directory containing multiple pdf,xml and png files.
let ftpStringUrl = "ftp://username:password@X.XXX.XXX.XXX:21/ABC/"
let urlString = ftpStringUrl.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
// First get and check the URL.
let url: NSURL! = NSURL(string: urlString!)
let success = (url != nil)
// If the URL is bogus, let the user know. Otherwise kick off the connection.
if (!success) {
NSLog("Invalid URL");
} else {
NSLog("Valid URL");
request = NSURLRequest(URL: url)
connection = NSURLConnection(request: request, delegate: self)
}
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
print(response);
}
func receiveDidStopWithStatus(statusString: String?){
}
func stopReceiveWithStatus(statusString: String?) {
print(data.length)
if (self.connection != nil) {
self.connection.cancel()
self.connection = nil
}
if (self.fileStream != nil) {
self.fileStream.close()
self.fileStream = nil
}
self.receiveDidStopWithStatus(statusString);
self.filePath = nil;
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
self.data.appendData(data)
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
self.stopReceiveWithStatus("Connection failed")
print("connection failed....")
}
func connectionDidFinishLoading(connection: NSURLConnection) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
let filePath = documentsPath.stringByAppendingPathComponent("")
data.writeToFile(filePath, atomically: true)
print("\(filePath)")
self.stopReceiveWithStatus(nil)
}
}
is there any way that I can download multiple files of different format from the Directory "ABC"? Thanks in advance.
I could be wrong, but I don't think it is possible to get an FTP directory listing using NSURLConnection or NSURLSession. You'll probably have to use CFFTPStream so that you can issue arbitrary FTP commands (e.g. LIST).
After that, you would just issue a normal URL loading request (with NSURLConnection or NSURLSession) for each file.
Alternatively, if you have control over the data on the server, you might consider one of the following alternatives: