iPhone reachability checking

66,072

Solution 1

From your screen shot, it seems like you do not have Reachability added to your project. You must download Reachability from Apple:

https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html

And add both .h and .m files to your project.

Update: You noted you have Reachability. But looking at the most recent version, I can see why you have the errors you listed - they changed the API and you are probably using sample code you found somewhere else. Try:

in .h file:

//import Reachability class
#import "Reachability.h"

// declare Reachability, you no longer have a singleton but manage instances
Reachability* reachability;

in .m file:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

 if(remoteHostStatus == NotReachable) {NSLog(@"no");}
else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); }

.....

- (void) handleNetworkChange:(NSNotification *)notice
{

  NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

   if(remoteHostStatus == NotReachable) {NSLog(@"no");}
else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); }
}

Solution 2

[reachability setHostName:@"http://www.google.com"];

Attention! I encountered the problem that it's always "NotReachable" if the http:// prefix is used.

Raphael

Solution 3

Here's the right code as it works for me today!!!

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];

reachability = [Reachability reachabilityForInternetConnection];

[reachability startNotifier];

NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

if(remoteHostStatus == NotReachable) {NSLog(@"no");}
else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); }

Solution 4

Do you have the following code anywhere?

[reachability startNotifier];

if your reachability code is from apple's example, then you need to do that before it can start reporting status updates to you.

Share:
66,072
Sneakyness
Author by

Sneakyness

Following my dreams, I mean it. Contact me: Email Address: [email protected] Aim: UnsavedDocument GitHub Twitter

Updated on May 18, 2020

Comments

  • Sneakyness
    Sneakyness almost 4 years

    I've found several examples of code to do what I want (check for reachability), but none of it seems to be exact enough to be of use to me. I can't figure out why this doesn't want to play nice.

    I have the reachability.h/m in my project, I'm doing

    #import <SystemConfiguration/SystemConfiguration.h>
    

    And I have the framework added. I also have:

    #import "Reachability.h"
    

    at the top of the .m in which I'm trying to use the reachability.

    Reachability* reachability = [Reachability sharedReachability];
    [reachability setHostName:@"http://www.google.com"];    // set your host name here
    NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
    
    if(remoteHostStatus == NotReachable) {NSLog(@"no");}
    else if (remoteHostStatus == ReachableViaWiFiNetwork) {NSLog(@"wifi"); }
    else if (remoteHostStatus == ReachableViaCarrierDataNetwork) {NSLog(@"cell"); }
    

    This is giving me all sorts of problems. What am I doing wrong? I'm an alright coder, I just have a hard time when it comes time to figure out what needs to be put where to enable what I want to do, regardless if I want to know what I want to do or not. (So frustrating)

    Update: This is what's going on. This is in my viewcontroller, which I have the

    #import <SystemConfiguration/SystemConfiguration.h>
    

    and

    #import "Reachability.h"
    

    set up with. This is my least favorite part of programming by far.reachability problems
    (source: sneakyness.com)


    FWIW, we never ended up implementing this in our code. The two features that required internet access (entering the sweepstakes, and buying the dvd), were not main features. Nothing else required internet access.

    Instead of adding more code, we just set the background of both internet views to a notice telling the users they must be connected to the internet to use this feature. It was in theme with the rest of the application's interface, and was done well/tastefully. They said nothing about it during the approval process, however we did get a personal phone call to verify that we were giving away items that actually pertained to the movie. According to their usually vague agreement, you aren't allowed to have sweepstakes otherwise.

    I would also think this adheres more strictly to their "only use things if you absolutely need them" ideaology as well.

    Here's the iTunes link to the application, EvoScanner.