Reputation: 16212
I'm trying to find a way to check to see if my application has a connection to the internet for my application. I've tried using the various Reachability
libraries you can find on GitHub which cause compiling errors for my project. (Makes other libraries not available, for some reason) and I've also tried the JustHTTP
library for making a GET request to http://www.google.com but it fails every time.
I'm not exactly sure what the issue is here, but I'd like to know how some of you go about validating a connection to the internet before allowing users to use the application.
This question was marked as a duplicated and linked to a question which used the Reachability
library provided by AshleyMills
. Which I had stated I've attempted using these libraries already, but could not do to confliction errors in my project. The GET request code provided on this question is also out of date, and does not work on SWIFT2. This linked question also assumes that I do not need to support anything under iOS 9 which seems like a bad practice. My minimum support is iOS8
Upvotes: 1
Views: 1964
Reputation: 76
Swift 2.x (requires Reachability)
do {
let reachable = try Reachability.init(hostname: "https:\\www.google.com")
if(reachable.currentReachabilityStatus == Reachability.NetworkStatus.ReachableViaWWAN){
//Device is connected thru 3G/4G/LTE
}else if(reachable.currentReachabilityStatus == Reachability.NetworkStatus.ReachableViaWiFi){
//Device is connected thru WiFi
}else{
//Device is not connected to the internet
}
}catch{
//Device is not connected to the internet
}
You can get Reachability here
Upvotes: 1
Reputation: 394
If your request fail all the time maybe it's because you are not doing them over https but over http. It has to do with ATS on iOS 9 - this blog explains very well how to disable (if you want - not recommended) ATS http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/
Upvotes: 0
Reputation: 6768
This code might help. You need the Reachability lib, this snippet can give you an example to check if your device reaches Internet via WiFi or via 4G/3G/LTE
func checkNetworkStatus() -> Bool {
let reachability: Reachability = Reachability.reachabilityForInternetConnection()
let networkStatus = reachability.currentReachabilityStatus().rawValue;
var isAvailable = false;
switch networkStatus {
case (NotReachable.rawValue):
isAvailable = false;
break;
case (ReachableViaWiFi.rawValue):
isAvailable = true;
break;
case (ReachableViaWWAN.rawValue):
isAvailable = true;
break;
default:
isAvailable = false;
break;
}
return isAvailable;
}
Upvotes: 0
Reputation: 944
Add Reachability.swift to your project.
And try below code.
do
{
let connected: Bool = try Reachability.reachabilityForInternetConnection().isReachable()
if connected == true
{
print("Internet connection OK")
}
else
{
print("Internet connection FAILED")
let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
}
catch
{
print("Handle Exception")
}
Upvotes: 0