Simpliest solution to check if File exists on a we

2019-01-14 20:19发布

问题:

There are a lot of discussion about this and I understand the solution to use the delegate method and check the response "404":

var request : NSURLRequest = NSURLRequest(URL: url)

var connection : NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
//...
}

But I would like to have a simple solution like:

var exists:Bool=fileexists(sURL);

Because I will have a lot of request in the same class with the delegate and I only want to check the response with my function fileexists().

Any hints ?

UPDATE I guess I'll have to do a synchronious request like the following, but I get always 0x0000000000000000 as a response::

   let urlPath: String = sURL;
    var url: NSURL = NSURL(string: urlPath)!
    var request1: NSURLRequest = NSURLRequest(URL: url)
    var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?
    >=nil
    var error: NSErrorPointer = nil
    var dataVal: NSData =  NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)!
    var err: NSError
    println(response)

回答1:

Checking if a resource exists on a server requires sending a HTTP request and receiving the response. TCP communication can take some amount of time, e.g. if the server is busy, some router between the client and the server does not work correctly, the network is down etc.

That's why asynchronous requests are always preferred. Even if you think that the request should take only milliseconds, it might sometimes be seconds due to some network problems. And – as we all know – blocking the main thread for some seconds is a big no-no.

All that being said, here is a possible implementation for a fileExists() method. You should not use it on the main thread, you have been warned!

The HTTP request method is set to "HEAD", so that the server sends only the response header, but no data.

func fileExists(url : NSURL!) -> Bool {

    let req = NSMutableURLRequest(URL: url)
    req.HTTPMethod = "HEAD"
    req.timeoutInterval = 1.0 // Adjust to your needs

    var response : NSURLResponse?
    NSURLConnection.sendSynchronousRequest(req, returningResponse: &response, error: nil)

    return ((response as? NSHTTPURLResponse)?.statusCode ?? -1) == 200
}


回答2:

Swift 3.0 version of Martin R's answer written asynchronously (the main thread isn't blocked):

func fileExistsAt(url : URL, completion: @escaping (Bool) -> Void) {
    let checkSession = Foundation.URLSession.shared
    var request = URLRequest(url: url)
    request.httpMethod = "HEAD"
    request.timeoutInterval = 1.0 // Adjust to your needs

    let task = checkSession.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if let httpResp: HTTPURLResponse = response as? HTTPURLResponse {
            completion(httpResp.statusCode == 200)
        }
    })

    task.resume()
}