+ Reply to Thread
Results 1 to 4 of 4

How to check if there is internet connection on iPhone OS devices

This is a discussion on How to check if there is internet connection on iPhone OS devices within the iPhone/iPad Programming Tutorials forums, part of the Programming Section category; Hey guys, Here I'll show you how to detect if there is no internet connection on iPhone device which is ...

          
   
  1. #1
    The Man Behind It All Fahad's Avatar
    Join Date
    Jun 2009
    Posts
    790

    Default How to check if there is internet connection on iPhone OS devices

    Hey guys,

    Here I'll show you how to detect if there is no internet connection on iPhone device which is using the device and give an error message if it's not there.

    First you need to download the Reachability from apple's site.

    1) Import Reachability.h, Reachability.m and CFNetwork, SystemConfiguration Frameworks to your project.

    2) Import Reachability.h in your .m file by putting this code at start of your file:
    Code:
     #import "Reachability.h"
    3) now inside your viewDidLoad insert this code:
    Code:
     Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
    	
    	NetworkStatus internetStatus = [r currentReachabilityStatus];
    	
    	if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    	{
    		UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
    		[myAlert show];
    		
    			}
    This code will check if it can connect to google.com and if now will make a UIAlertView with title "No Internet Connection" and message "This app require an internet connection via WiFi or cellular network to work."


    That's it if you need any help implementing it, leave a replay.
    Last edited by Fahad; 01-10-2011 at 12:11 PM.



    MozyMac Founder,Chairman and CEO

    MacBook Pro Unibody late 2008 2.4Ghz 4GB ram 250GB HD

    MozyMac Youtube Channel

  2. #2
    Junior Member Creegan's Avatar
    Join Date
    Oct 2010
    Location
    Heredia, Costa Rica
    Posts
    2

    Lightbulb Just a little fix

    Quote Originally Posted by Fahad View Post
    Hey guys,

    Here I'll show you how to detect if there is no internet connection on iPhone device which is using the device and give an error message if it's not there.

    First you need to download the Reachability from apple's site.

    1) Import Reachability.h, Reachability.m and CFNetwork, SystemConfiguration Frameworks to your project.

    2) Import Reachability.h in your .m file by putting this code at start of your file:
    Code:
     #import "Reachability.h"
    3) now inside your viewDidLoad insert this code:
    Code:
     Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
    	
    	NetworkStatus internetStatus = [r currentReachabilityStatus];
    	
    	if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    	{
    		UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
    		[myAlert show];
    		[myAlert release];
    		
    			}
    This code will check if it can connect to google.com and if now will make a UIAlertView with title "No Internet Connection" and message "This app require an internet connection via WiFi or cellular network to work."


    That's it if you need any help implementing it, leave a replay.
    Just a quick note, this might crash if this is implemented in a controller other than the AppDelegate Controller, this because you are marking the alert window for "autorelease" and the you are calling the "release" method; thus; taking the "retainCount" below 0 since it was never retained.

    You should either NOT mark it for "autorelease" or remove the "release" message.

    Thanks for the share on this subject
    Highly appreciated.
    Last edited by Creegan; 10-06-2010 at 03:18 PM.

  3. #3
    The Man Behind It All Fahad's Avatar
    Join Date
    Jun 2009
    Posts
    790

    Default

    Quote Originally Posted by Creegan View Post
    Just a quick note, this might crash if this is implemented in a controller other than the AppDelegate Controller, this because you are marking the alert window for "autorelease" and the you are calling the "release" method; thus; taking the "retainCount" below 0 since it was never retained.

    You should either NOT mark it for "autorelease" or remove the "release" message.

    Thanks for the share on this subject
    Highly appreciated.

    Thanks for the info, actually i was wondering what was the reason of the crash if there isn't any internet connection.



    MozyMac Founder,Chairman and CEO

    MacBook Pro Unibody late 2008 2.4Ghz 4GB ram 250GB HD

    MozyMac Youtube Channel

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    10

    Default

    Thank You its a Really Nice Service,I am not Aware with this Details.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts