mcfly soft mcfly soft - 1 year ago 127
Swift Question

Simpliest solution to check if File exists on a webserver. (Swift)

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)!

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 ?

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?
var error: NSErrorPointer = nil
var dataVal: NSData = NSURLConnection.sendSynchronousRequest(request1, returningResponse: response, error:nil)!
var err: NSError

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download