Skip to content

Instantly share code, notes, and snippets.

@eugenpirogoff
Created October 21, 2015 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugenpirogoff/e0413b6f8a17025cc402 to your computer and use it in GitHub Desktop.
Save eugenpirogoff/e0413b6f8a17025cc402 to your computer and use it in GitHub Desktop.
Check if there is a network connection
import Foundation
public class Reachability {
class func isConnectedToNetwork()->Bool{
var Status:Bool = false
let url = NSURL(string: "http://google.com/")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "HEAD"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
var response: NSURLResponse?
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
Status = true
}
}
return Status
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment