Reputation: 16276
I am using the updated Reachability library to test whether the internet connexion is reachable. I try to Log a message in case the internet is not reachable, but the Log doesn't debug:
//Test the internet connection
Reachability* reach = [Reachability reachabilityForInternetConnection];
reach.unreachableBlock = ^(Reachability*reach)
{
NSLog(@"Internet connexion unreachable");//Although Internet cnx is off, this message is not displayed
return;
};
// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];
Am I misunderstanding the Reachability library? How to perform a given task when Internet is off? Thanx.
P.S: My iPad is only wifi, without 3G service.
Upvotes: 1
Views: 1375
Reputation: 139
Using Alamofire to check interner, In Swift 4
import Foundation
import Alamofire
class Connectivity {
class func isConnectedToInternet() ->Bool {
return NetworkReachabilityManager()!.isReachable
}
}
Then call this function
if Connectivity.isConnectedToInternet() {
print("Yes! internet is available.")
// do some tasks..
}
Upvotes: 2
Reputation: 16276
Ok, so best way I got is following Apple sample code:
//Test the internet connection
Reachability* reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
//See if the reachable object status is "ReachableViaWifi"
if (netStatus!=ReachableViaWiFi) {
//If not
NSLog(@"wifi unavailable");
//Alert the user about the Internet cnx
WBErrorNoticeView *notice = [WBErrorNoticeView errorNoticeInView:self.view title:@"Network Error" message:@"Check your internet connection."];
notice.sticky = NO;
[notice show];
return;//Exit the method
}
Upvotes: 0
Reputation: 3991
Actually, you should register for a notification to receive changed reachibility :
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
Look at Apple example here : http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_ReachabilityAppDelegate_m.html#//apple_ref/doc/uid/DTS40007324-Classes_ReachabilityAppDelegate_m-DontLinkElementID_4
Upvotes: 0