Reputation: 47302
I would like to check to see if I have an Internet connection on iOS using the Cocoa Touch libraries or on macOS using the Cocoa libraries.
I came up with a way to do this using an NSURL
. The way I did it seems a bit unreliable (because even Google could one day be down and relying on a third party seems bad), and while I could check to see for a response from some other websites if Google didn't respond, it does seem wasteful and an unnecessary overhead on my application.
- (BOOL)connectedToInternet {
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
return ( URLString != NULL ) ? YES : NO;
}
Is what I have done bad, (not to mention stringWithContentsOfURL
is deprecated in iOS 3.0 and macOS 10.4) and if so, what is a better way to accomplish this?
Upvotes: 1412
Views: 518206
Reputation: 612
Swift 5 and later:
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress, {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
}) else {
return false
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
Call this class like this:
if Reachability.isConnectedToNetwork() == true {
// Do something
} else {
// Do something
}
Upvotes: 7
Reputation: 9141
When using iOS 12 or macOS v10.14 (Mojave) or newer, you can use NWPathMonitor
instead of the pre-historic Reachability
class. As a bonus you can easily detect the current network connection type:
import Network // Put this on top of your class
let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
if path.status != .satisfied {
// Not connected
}
else if path.usesInterfaceType(.cellular) {
// Cellular 3/4/5g connection
}
else if path.usesInterfaceType(.wifi) {
// Wi-Fi connection
}
else if path.usesInterfaceType(.wiredEthernet) {
// Ethernet connection
}
}
monitor.start(queue: DispatchQueue.global(qos: .background))
More info here: https://developer.apple.com/documentation/network/nwpathmonitor
Upvotes: 37
Reputation: 34341
Swift 5, Alamofire, host
// Session reference
var alamofireSessionManager: Session!
func checkHostReachable(completionHandler: @escaping (_ isReachable:Bool) -> Void) {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 1
configuration.timeoutIntervalForResource = 1
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
alamofireSessionManager = Session(configuration: configuration)
alamofireSessionManager.request("https://google.com").response { response in
completionHandler(response.response?.statusCode == 200)
}
}
// Using
checkHostReachable() { (isReachable) in
print("isReachable:\(isReachable)")
}
Upvotes: 0
Reputation: 1014
For my iOS projects, I recommend using
Reachability Class
Declared in Swift. For me, it works simply fine with
Wi-Fi and Cellular data
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
let ret = (isReachable && !needsConnection)
return ret
}
}
Use a conditional statement,
if Reachability.isConnectedToNetwork() {
// Enter your code here
}
}
else {
print("NO Internet connection")
}
This class is useful in almost every case your app uses the Internet connection. Such as if the condition is true, API can be called or task could be performed.
Upvotes: 11
Reputation: 1192
Check Internet connection availability in (iOS) using Xcode 9 and Swift 4.0
Follow the below steps
Step 1:
Create an extension file and give it the name ReachabilityManager.swift. Then add the lines of code below.
import Foundation
import SystemConfiguration
public class ConnectionCheck
{
class func isConnectedToNetwork() -> Bool
{
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(to: &zeroAddress,
{
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
SCNetworkReachabilityCreateWithAddress(nil, $0)
}
})
else {
return false
}
var flags: SCNetworkReachabilityFlags = []
if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) {
return false
}
let isReachable = flags.contains(.reachable)
let needsConnection = flags.contains(.connectionRequired)
return (isReachable && !needsConnection)
}
}
Step 2: Call the extension above using the code below.
if ConnectionCheck.isConnectedToNetwork()
{
print("Connected")
// Online related Business logic
}
else{
print("disConnected")
// Offline related business logic
}
Upvotes: 3
Reputation: 1962
I know the question is asking for Coca Touch solution, but I want to provide a solution for people who searched check the Internet connection on iOS and will have one more option here.
If you are already using Alamofire, here is what you can benefit from that.
You can add following class to your app, and call MNNetworkUtils.main.isConnected()
to get a Boolean value on whether it’s connected or not.
#import Alamofire
class MNNetworkUtils {
static let main = MNNetworkUtils()
init() {
manager = NetworkReachabilityManager(host: "google.com")
listenForReachability()
}
private let manager: NetworkReachabilityManager?
private var reachable: Bool = false
private func listenForReachability() {
self.manager?.listener = { [unowned self] status in
switch status {
case .notReachable:
self.reachable = false
case .reachable(_), .unknown:
self.reachable = true
}
}
self.manager?.startListening()
}
func isConnected() -> Bool {
return reachable
}
}
This is a singleton class. Every time, when the user connects or disconnects the network, it will override self.reachable
to true/false correctly, because we start listening for the NetworkReachabilityManager
on singleton initialization.
Also in order to monitor reachability, you need to provide a host. Currently, I am using google.com, but feel free to change to any other hosts or one of yours if needed. Change the class name and file name to anything matching your project.
Upvotes: 1
Reputation: 3008
Swift 3 / Swift 4
You must first import
import SystemConfiguration
You can check the Internet connection with the following method:
func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
Upvotes: 11
Reputation: 3936
This is for Swift 3.0 and async. Most answers are sync solution which is going to block your main thread if you have a very slow connection.
This solution is better, but not perfect because it rely on Google to check the connectivity, so feel free to use another URL.
func checkInternetConnection(completionHandler:@escaping (Bool) -> Void)
{
if let url = URL(string: "http://www.google.com/")
{
var request = URLRequest(url: url)
request.httpMethod = "HEAD"
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 5
let tast = URLSession.shared.dataTask(with: request, completionHandler:
{
(data, response, error) in
completionHandler(error == nil)
})
tast.resume()
}
else
{
completionHandler(true)
}
}
Upvotes: 3
Reputation: 4905
Please try this. It will help you (Swift 4)
Install Reachability via CocoaPods or Carthage: Reachability
Import Reachability and use it in the Network class
import Reachability
class Network {
private let internetReachability : Reachability?
var isReachable : Bool = false
init() {
self.internetReachability = Reachability.init()
do{
try self.internetReachability?.startNotifier()
NotificationCenter.default.addObserver(self, selector: #selector(self.handleNetworkChange), name: .reachabilityChanged, object: internetReachability)
}
catch {
print("could not start reachability notifier")
}
}
@objc private func handleNetworkChange(notify: Notification) {
let reachability = notify.object as! Reachability
if reachability.connection != .none {
self.isReachable = true
}
else {
self.isReachable = false
}
print("Internet Connected : \(self.isReachable)") //Print Status of Network Connection
}
}
Use like the below where you need it.
var networkOBJ = Network()
// Use "networkOBJ.isReachable" for Network Status
print(networkOBJ.isReachable)
Upvotes: 1
Reputation: 46713
Important: This check should always be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you'll freeze up your app.
Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift
Test reachability via closures
let reachability = Reachability()!
reachability.whenReachable = { reachability in
if reachability.connection == .wifi {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
}
reachability.whenUnreachable = { _ in
print("Not reachable")
}
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
Add SystemConfiguration
framework to the project but don't worry about including it anywhere
Add Tony Million's version of Reachability.h
and Reachability.m
to the project (found here: https://github.com/tonymillion/Reachability)
Update the interface section
#import "Reachability.h"
// Add this to the interface in the .m file of your view controller
@interface MyViewController ()
{
Reachability *internetReachableFoo;
}
@end
Then implement this method in the .m file of your view controller which you can call
// Checks if we have an internet connection or not
- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
});
};
[internetReachableFoo startNotifier];
}
Important Note: The Reachability
class is one of the most used classes in projects so you might run into naming conflicts with other projects. If this happens, you'll have to rename one of the pairs of Reachability.h
and Reachability.m
files to something else to resolve the issue.
Note: The domain you use doesn't matter. It's just testing for a gateway to any domain.
Upvotes: 1321
Reputation: 4799
Import Reachable.h
class in your ViewController
, and use the following code to check connectivity:
#define hasInternetConnection [[Reachability reachabilityForInternetConnection] isReachable]
if (hasInternetConnection){
// To-do block
}
Upvotes: 6
Reputation: 49
//
// Connectivity.swift
//
//
// Created by Kausik Jati on 17/07/20.
//
//
import Foundation
import Network
enum ConnectionState: String {
case notConnected = "Internet connection not avalable"
case connected = "Internet connection avalable"
case slowConnection = "Internet connection poor"
}
protocol ConnectivityDelegate: class {
func checkInternetConnection(_ state: ConnectionState, isLowDataMode: Bool)
}
class Connectivity: NSObject {
private let monitor = NWPathMonitor()
weak var delegate: ConnectivityDelegate? = nil
private let queue = DispatchQueue.global(qos: .background)
private var isLowDataMode = false
static let instance = Connectivity()
private override init() {
super.init()
monitor.start(queue: queue)
startMonitorNetwork()
}
private func startMonitorNetwork() {
monitor.pathUpdateHandler = { path in
if #available(iOS 13.0, *) {
self.isLowDataMode = path.isConstrained
} else {
// Fallback on earlier versions
self.isLowDataMode = false
}
if path.status == .requiresConnection {
print("requiresConnection")
self.delegate?.checkInternetConnection(.slowConnection, isLowDataMode: self.isLowDataMode)
} else if path.status == .satisfied {
print("satisfied")
self.delegate?.checkInternetConnection(.connected, isLowDataMode: self.isLowDataMode)
} else if path.status == .unsatisfied {
print("unsatisfied")
self.delegate?.checkInternetConnection(.notConnected, isLowDataMode: self.isLowDataMode)
}
}
}
func stopMonitorNetwork() {
monitor.cancel()
}
}
Upvotes: 1
Reputation: 5902
Here is how I do it in my apps: While a 200 status response code doesn't guarantee anything, it is stable enough for me. This doesn't require as much loading as the NSData answers posted here, as mine just checks the HEAD response.
func checkInternet(flag:Bool, completionHandler:(internet:Bool) -> Void)
{
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
let url = NSURL(string: "http://www.google.com/")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "HEAD"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
NSURLConnection.sendAsynchronousRequest(request, queue:NSOperationQueue.mainQueue(), completionHandler:
{(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
let rsp = response as! NSHTTPURLResponse?
completionHandler(internet:rsp?.statusCode == 200)
})
}
func yourMethod()
{
self.checkInternet(false, completionHandler:
{(internet:Bool) -> Void in
if (internet)
{
// "Internet" aka Google URL reachable
}
else
{
// No "Internet" aka Google URL un-reachable
}
})
}
typedef void(^connection)(BOOL);
- (void)checkInternet:(connection)block
{
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url];
headRequest.HTTPMethod = @"HEAD";
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
defaultConfigObject.timeoutIntervalForResource = 10.0;
defaultConfigObject.requestCachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:headRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (!error && response)
{
block([(NSHTTPURLResponse *)response statusCode] == 200);
}
}];
[dataTask resume];
}
- (void)yourMethod
{
[self checkInternet:^(BOOL internet)
{
if (internet)
{
// "Internet" aka Google URL reachable
}
else
{
// No "Internet" aka Google URL un-reachable
}
}];
}
Upvotes: 74
Reputation: 988
Pod `Alamofire` has `NetworkReachabilityManager`, you just have to create one function
func isConnectedToInternet() ->Bool {
return NetworkReachabilityManager()!.isReachable
}
Upvotes: 2
Reputation: 9877
You could use Reachability
by (available here).
#import "Reachability.h"
- (BOOL)networkConnection {
return [[Reachability reachabilityWithHostName:@"www.google.com"] currentReachabilityStatus];
}
if ([self networkConnection] == NotReachable) { /* No Network */ } else { /* Network */ } //Use ReachableViaWiFi / ReachableViaWWAN to get the type of connection.
Upvotes: 48
Reputation: 3191
Using Apple's Reachability code, I created a function that'll check this correctly without you having to include any classes.
Include the SystemConfiguration.framework in your project.
Make some imports:
#import <sys/socket.h>
#import <netinet/in.h>
#import <SystemConfiguration/SystemConfiguration.h>
Now just call this function:
/*
Connectivity testing code pulled from Apple's Reachability Example: https://developer.apple.com/library/content/samplecode/Reachability
*/
+(BOOL)hasConnectivity {
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress);
if (reachability != NULL) {
//NetworkStatus retVal = NotReachable;
SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(reachability, &flags)) {
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
// If target host is not reachable
return NO;
}
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
{
// If target host is reachable and no connection is required
// then we'll assume (for now) that your on Wi-Fi
return YES;
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
// ... and the connection is on-demand (or on-traffic) if the
// calling application is using the CFSocketStream or higher APIs.
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
// ... and no [user] intervention is needed
return YES;
}
}
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
// ... but WWAN connections are OK if the calling application
// is using the CFNetwork (CFSocketStream?) APIs.
return YES;
}
}
}
return NO;
}
And it's iOS 5 tested for you.
Upvotes: 147
Reputation: 2700
Checking the Internet connection availability in (iOS) Xcode 8 , Swift 3.0
This is simple method for checking the network availability like our device is connected to any network or not. I have managed to translate it to Swift 3.0 and here the final code. The existing Apple Reachability class and other third party libraries seemed to be too complicated to translate to Swift.
This works for both 3G,4G and WiFi connections.
Don’t forget to add “SystemConfiguration.framework” to your project builder.
//Create new swift class file Reachability in your project.
import SystemConfiguration
public class InternetReachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0)).takeRetainedValue()
}
var flags: SCNetworkReachabilityFlags = 0
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
return false
}
let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return isReachable && !needsConnection
}
}
// Check network connectivity from anywhere in project by using this code.
if InternetReachability.isConnectedToNetwork() == true {
print("Internet connection OK")
} else {
print("Internet connection FAILED")
}
Upvotes: 6
Reputation: 1497
Try this:
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
if ([self.delegate respondsToSelector:@selector(getErrorResponse:)]) {
[self.delegate performSelector:@selector(getErrorResponse:) withObject:@"No Network Connection"];
}
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"BMC" message:@"No Network Connection" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
[alertView show];
}
Upvotes: -2
Reputation: 499
Step 3: Create the below function
- (BOOL)checkNetConnection {
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
NetworkStatus netStatus = [self.internetReachability currentReachabilityStatus];
switch (netStatus) {
case NotReachable:
{
return NO;
}
case ReachableViaWWAN:
{
return YES;
}
case ReachableViaWiFi:
{
return YES;
}
}
}
Step 4: Call the function as below:
if (![self checkNetConnection]) {
[GlobalFunctions showAlert:@""
message:@"Please connect to the Internet!"
canBtntitle:nil
otherBtnTitle:@"Ok"];
return;
}
else
{
Log.v("internet is connected","ok");
}
Upvotes: 6
Reputation: 279
Download the Reachability file, https://gist.github.com/darkseed/1182373
And add CFNetwork.framework
and 'SystemConfiguration.framework' in framework
Do #import "Reachability.h"
First: Add CFNetwork.framework
in framework
Code: ViewController.m
- (void)viewWillAppear:(BOOL)animated
{
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
/// Create an alert if connection doesn't work
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"No Internet Connection" message:NSLocalizedString(@"InternetMessage", nil)delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
else
{
NSLog(@"INTERNET IS CONNECT");
}
}
Upvotes: 10
Reputation: 21452
I think this one is the best answer.
"Yes" means connected. "No" means disconnected.
#import "Reachability.h"
- (BOOL)canAccessInternet
{
Reachability *IsReachable = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStats = [IsReachable currentReachabilityStatus];
if (internetStats == NotReachable)
{
return NO;
}
else
{
return YES;
}
}
Upvotes: 9
Reputation: 11247
Very simple.... Try these steps:
Step 1: Add the SystemConfiguration
framework into your project.
Step 2: Import the following code into your header
file.
#import <SystemConfiguration/SystemConfiguration.h>
Step 3: Use the following method
Type 1:
- (BOOL) currentNetworkStatus {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
BOOL connected;
BOOL isConnected;
const char *host = "www.apple.com";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
SCNetworkReachabilityFlags flags;
connected = SCNetworkReachabilityGetFlags(reachability, &flags);
isConnected = NO;
isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
return isConnected;
}
Type 2:
Import header : #import "Reachability.h"
- (BOOL)currentNetworkStatus
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return networkStatus != NotReachable;
}
Step 4: How to use:
- (void)CheckInternet
{
BOOL network = [self currentNetworkStatus];
if (network)
{
NSLog(@"Network Available");
}
else
{
NSLog(@"No Network Available");
}
}
Upvotes: 16
Reputation: 487
First: Add CFNetwork.framework
in framework
Code: ViewController.m
#import "Reachability.h"
- (void)viewWillAppear:(BOOL)animated
{
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
/// Create an alert if connection doesn't work
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"No Internet Connection" message:NSLocalizedString(@"InternetMessage", nil)delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[myAlert show];
[myAlert release];
}
else
{
NSLog(@"INTERNET IS CONNECT");
}
}
Upvotes: 11
Reputation: 4609
To do this yourself is extremely simple. The following method will work. Just be sure to not allow a hostname protocol such as HTTP, HTTPS, etc. to be passed in with the name.
-(BOOL)hasInternetConnection:(NSString*)urlAddress
{
SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [urlAddress UTF8String]);
SCNetworkReachabilityFlags flags;
if (!SCNetworkReachabilityGetFlags(ref, &flags))
{
return NO;
}
return flags & kSCNetworkReachabilityFlagsReachable;
}
It is quick simple and painless.
Upvotes: 9
Reputation: 3990
There is also another method to check Internet connection using the iPhone SDK.
Try to implement the following code for the network connection.
#import <SystemConfiguration/SystemConfiguration.h>
#include <netdb.h>
/**
Checking for network availability. It returns
YES if the network is available.
*/
+ (BOOL) connectedToNetwork
{
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability =
SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
printf("Error. Could not recover network reachability flags\n");
return NO;
}
BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
return (isReachable && !needsConnection) ? YES : NO;
}
Upvotes: 9
Reputation: 516
Get the Reachabilty class from https://github.com/tonymillion/Reachability, add the system configuration framework in your project, do import Reachability.h in your class and implement the custom methods as below:
- (BOOL)isConnectedToInternet
{
//return NO; // Force for offline testing
Reachability *hostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
return !(netStatus == NotReachable);
}
Upvotes: 3
Reputation: 32320
The Reachability class is OK to find out if the Internet connection is available to a device or not...
But in case of accessing an intranet resource:
Pinging the intranet server with the reachability class always returns true.
So a quick solution in this scenario would be to create a web method called pingme
along with other webmethods on the service.
The pingme
should return something.
So I wrote the following method on common functions
-(BOOL)PingServiceServer
{
NSURL *url=[NSURL URLWithString:@"http://www.serveraddress/service.asmx/Ping"];
NSMutableURLRequest *urlReq=[NSMutableURLRequest requestWithURL:url];
[urlReq setTimeoutInterval:10];
NSURLResponse *response;
NSError *error = nil;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:urlReq
returningResponse:&response
error:&error];
NSLog(@"receivedData:%@",receivedData);
if (receivedData !=nil)
{
return YES;
}
else
{
NSLog(@"Data is null");
return NO;
}
}
The above method was so useful for me, so whenever I try to send some data to the server I always check the reachability of my intranet resource using this low timeout URLRequest.
Upvotes: 8
Reputation: 306
First download the reachability class and put reachability.h and reachabilty.m file in your Xcode.
The best way is to make a common Functions class (NSObject) so that you can use it any class. These are two methods for a network connection reachability check:
+(BOOL) reachabiltyCheck
{
NSLog(@"reachabiltyCheck");
BOOL status =YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
Reachability * reach = [Reachability reachabilityForInternetConnection];
NSLog(@"status : %d",[reach currentReachabilityStatus]);
if([reach currentReachabilityStatus]==0)
{
status = NO;
NSLog(@"network not connected");
}
reach.reachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
});
};
reach.unreachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
});
};
[reach startNotifier];
return status;
}
+(BOOL)reachabilityChanged:(NSNotification*)note
{
BOOL status =YES;
NSLog(@"reachabilityChanged");
Reachability * reach = [note object];
NetworkStatus netStatus = [reach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
status = NO;
NSLog(@"Not Reachable");
}
break;
default:
{
if (!isSyncingReportPulseFlag)
{
status = YES;
isSyncingReportPulseFlag = TRUE;
[DatabaseHandler checkForFailedReportStatusAndReSync];
}
}
break;
}
return status;
}
+ (BOOL) connectedToNetwork
{
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
NSLog(@"Error. Could not recover network reachability flags");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}
Now you can check network connection in any class by calling this class method.
Upvotes: 9
Reputation: 2127
- (void)viewWillAppear:(BOOL)animated
{
NSString *URL = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
return (URL != NULL ) ? YES : NO;
}
Or use the Reachability class.
There are two ways to check Internet availability using the iPhone SDK:
1. Check the Google page is opened or not.
2. Reachability Class
For more information, please refer to Reachability (Apple Developer).
Upvotes: 12
Reputation: 2148
Here's a very simple answer:
NSURL *scriptUrl = [NSURL URLWithString:@"http://www.google.com/m"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data)
NSLog(@"Device is connected to the Internet");
else
NSLog(@"Device is not connected to the Internet");
The URL should point to an extremely small website. I use Google's mobile website here, but if I had a reliable web server I'd upload a small file with just one character in it for maximum speed.
If checking whether the device is somehow connected to the Internet is everything you want to do, I'd definitely recommend using this simple solution. If you need to know how the user is connected, using Reachability is the way to go.
Careful: This will briefly block your thread while it loads the website. In my case, this wasn't a problem, but you should consider this (credits to Brad for pointing this out).
Upvotes: 83