Created
October 21, 2015 15:34
-
-
Save eugenpirogoff/e0413b6f8a17025cc402 to your computer and use it in GitHub Desktop.
Check if there is a network connection
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
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