Reputation: 4330
I'm trying to detect if the phone has internet access or not. I stackoverflowed it and every time i use a library (https://github.com/ashleymills/Reachability.swift) or i use recomended class methods it ONLY works on iOS 9.0, but iOS 8 it just returns false (at least in the simulator, i dont know in a real phone). Any ideas?
Upvotes: 0
Views: 686
Reputation: 883
Had the same problem. For the life of me, I couldn't figure out the reason why, so had to implement this workaround piece of code that works for both iOS 9 and iOS 8:
class func isConnectedToNetwork()-> Bool {
var isConnected:Bool = false
let request = NSMutableURLRequest(URL: NSURL(string: "http://google.com/")!)
request.HTTPMethod = "HEAD"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
var response: NSURLResponse?
do {
try _ = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response) as NSData?
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
isConnected = true
}
}
}
catch {
print(error)
}
return isConnected
}
Obviously, if Google falls - so does the workaround. But I'm taking that risk :)
Based on answer: https://stackoverflow.com/a/29050673/2941553
Upvotes: 1