---
title: "How to check for an active Internet Connection on iPhone SDK?"  
description: "How to check for an active Internet Connection on iPhone SDK?"  
author: "Anonymous User"  
published: 2015-04-25  
updated: 2015-04-25  
canonical: https://www.mindstick.com/forum/23135/how-to-check-for-an-active-internet-connection-on-iphone-sdk  
category: "iphone"  
tags: ["iphone", "iphone sdk"]  
reading_time: 4 minutes  

---

# How to check for an active Internet Connection on iPhone SDK?

I would like to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) to see if I have an [Internet connection](https://www.mindstick.com/blog/299301/how-to-test-your-internet-connection-s-quality-over-time) on the iPhone using the Cocoa Touch libraries.\
I [came up](https://answers.mindstick.com/qa/41115/who-came-up-with-the-10-percent-plan-what-did-this-plan-say) with a way to do this using an NSURL. The way I did it seems a bit unreliable (because even [Google](https://www.mindstick.com/articles/43833/google-lighthouse-and-how-is-it-changing-the-way-we-development-and-design-websites) could [one day](https://answers.mindstick.com/qa/39936/in-2006-which-player-signed-a-one-day-contract-with-the-american-football-team-san-francisco-49ers-then-retired) be down and relying on a 3rd party seems bad) and while I could check to see for a [response](https://www.mindstick.com/forum/12719/how-to-make-response-write-display-special-character-like-lt-gt) 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](https://www.mindstick.com/forum/972/deprecated-as-a-property-in-sencha-touch) in [3.0](https://answers.mindstick.com/qa/49733/what-is-web-version-web-1-0-2-0-and-3-0)) And if so what is a better way to accomplish this?\

## Replies

### Reply by Anonymous User

1) Add SystemConfiguration framework to the project but don't worry about including it anywhere\
2) Add Apple's version of Reachability.h and Reachability.m to the project (you can get those here)\
3) Add @class Reachability; to the .h file of where you are implementing the code\
4) Create a couple instances to check in the interface section of the .h file:\
Reachability* internetReachable;Reachability* hostReachable;5) Add a method in the .h for when the network status updates:\
-(void) checkNetworkStatus:(NSNotification *)notice;6) Add #import "Reachability.h" to the .m file where you are implementing the check\
7) In the .m file of where you are implementing the check, you can place this in one of the first methods called (init or viewWillAppear or viewDidLoad etc):\

```
-(void) viewWillAppear:(BOOL)animated{    // check for internet connection    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];    internetReachable = [Reachability reachabilityForInternetConnection];    [internetReachable startNotifier];    // check if a pathway to a random host exists    hostReachable = [Reachability reachabilityWithHostName:@"www.apple.com"];    [hostReachable startNotifier];    // now patiently wait for the notification}
```

\
8) Set up the method for when the notification gets sent and set whatever checks or call whatever methods you may have set up (in my case, I just set a BOOL)

```
-(void) checkNetworkStatus:(NSNotification *)notice{    // called after network status changes    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];    switch (internetStatus)    {        case NotReachable:        {            NSLog(@"The internet is down.");            self.internetActive = NO;            break;        }        case ReachableViaWiFi:        {            NSLog(@"The internet is working via WIFI.");            self.internetActive = YES;            break;        }        case ReachableViaWWAN:        {            NSLog(@"The internet is working via WWAN.");            self.internetActive = YES;            break;        }    }    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];    switch (hostStatus)    {        case NotReachable:        {            NSLog(@"A gateway to the host server is down.");            self.hostActive = NO;            break;        }        case ReachableViaWiFi:        {            NSLog(@"A gateway to the host server is working via WIFI.");            self.hostActive = YES;            break;        }        case ReachableViaWWAN:        {            NSLog(@"A gateway to the host server is working via WWAN.");            self.hostActive = YES;            break;        }    }}
```

9) In your dealloc or viewWillDisappear or similar method, remove yourself as an observer\

```
-(void) viewWillDisappear:(BOOL)animated{    [[NSNotificationCenter defaultCenter] removeObserver:self];}
```

\
Note: There might be an instance using viewWillDisappear where you receive a memory warning and the observer never gets unregistered so you should account for that as well.


---

Original Source: https://www.mindstick.com/forum/23135/how-to-check-for-an-active-internet-connection-on-iphone-sdk

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
