Using Babatunde's code sample but here is an updated version for Swift 2.0 and error handling:
EDIT: Also changed the URL for Google as HTTPS for iOS 9.
EDIT2: Original article: http://www.brianjcoleman.com/tutorial-check-for-internet-connection-in-swift/
import Foundation
import SystemConfiguration
public class Reachability {
// Check if internet connection is available
class func isConnectedToNetwork() -> Bool {
var status:Bool = false
let url = NSURL(string: "https://google.com")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "HEAD"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
var response:NSURLResponse?
do {
let _ = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response) as NSData?
}
catch let error as NSError {
print(error.localizedDescription)
}
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
status = true
}
}
return status
}
}