Created
June 23, 2015 02:31
-
-
Save burhanaksendir/9e427b6f62758d777c81 to your computer and use it in GitHub Desktop.
How to check if a remote file exists in Swift ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
deprecated, does anyone know how to make this in swift 4? It asks to use NSURLSESSION, but it refers to an Objective C function