I wanted this class to push or show an alertView saying it's not connected to Internet
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?
do{
var data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)
print(response)
} catch {
//handle error
print(error)
}
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
Status = true
}
}
return Status
}
}
Here is my declaration inside the mainViewcontroller(initial)
if Reachability.isConnectedToNetwork() == true {
}else {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = (self.storyboard?.instantiateViewControllerWithIdentifier("SignInSignUpVCIdentifier"))!
self.presentViewController(viewController, animated: true, completion: nil)
})
}//end of
It doesn't push anything. It's showing making attempt 2 sleeping for 4.34324