Skip to content

Instantly share code, notes, and snippets.

@burhanaksendir
Created June 23, 2015 02:31
Show Gist options
  • Save burhanaksendir/9e427b6f62758d777c81 to your computer and use it in GitHub Desktop.
Save burhanaksendir/9e427b6f62758d777c81 to your computer and use it in GitHub Desktop.
How to check if a remote file exists in Swift ?
func remoteFileExists(url: String) -> Bool {
var exists: Bool = false
let url: NSURL = NSURL(string: url)!
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "HEAD"
var response: NSURLResponse?
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response , error: nil)
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
exists = true
}else{
exists = false
}
}
return exists
}
@frederiknormann
Copy link

deprecated, does anyone know how to make this in swift 4? It asks to use NSURLSESSION, but it refers to an Objective C function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment