StuartM
StuartM

Reputation: 6813

Using reachability on Iphone simulator and no wifi

I am using Reachability in my IOS app to determine a connection.

Following from this post wifi on iphone simulator

If the wifi is turned off the internet connection for the simulator is not available but the phone is still connected to Wifi therefore a connection has not changed. This is all fine and understood and of course I can test on the device itself.

However, I am looking to handle the error that a user is connected to wifi but the wifi has no internet connection like the screenshot below.

I use reachability in the following way:

#pragma mark - ()
- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];

self.hostReach = [Reachability reachabilityWithHostname: @"api.parse.com"];
[self.hostReach startNotifier];

self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];

self.wifiReach = [Reachability reachabilityForLocalWiFi];
[self.wifiReach startNotifier];

}

//Called by Reachability whenever status changes. - (void)reachabilityChanged:(NSNotification* )note { Reachability *curReach = (Reachability *)[note object]; NSParameterAssert([curReach isKindOfClass: [Reachability class]]); NSLog(@"Reachability changed: %@", curReach); networkStatus = [curReach currentReachabilityStatus];

}

I am using Reachability for ARC from tonymillion's github here: https://github.com/tonymillion/Reachability

Does anyone know how I can handle this situation of connection with a better error? enter image description here

Upvotes: 2

Views: 4266

Answers (1)

freelancer
freelancer

Reputation: 1658

create following method in appdelegate to use it on any class.

-(BOOL)isHostAvailable
{
    //return NO; // force for offline testing
    Reachability *hostReach = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [hostReach currentReachabilityStatus];
    return !(netStatus == NotReachable);
}

Upvotes: 2

Related Questions