This question already has answers here:
How can I check for an active Internet connection on iOS or macOS?
(46 answers)
Closed 5 years ago.
I'm wondering how I can check if the user is connect to internet through WIFI or cellular data 3G or 4G.
Also I don't want to check if a website is reachable or not, the thing that I want to check if there is internet on the device or not. I tried to look over the internet all that I see is that they check if the website is reachable or not using the Rechability class.
I want to check if the user has internet or not when he opens my application.
I'm using Xcode6 with Objective-C.
Use this code and import Reachability.h file
if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]==NotReachable)
{
//connection unavailable
}
else
{
//connection available
}
First Download Reachability classes from this Link:
Rechability from Github
Add Instance of Reachability in AppDelegate.h
#property (nonatomic) Reachability *hostReachability;
#property (nonatomic) Reachability *internetReachability;
#property (nonatomic) Reachability *wifiReachability;
Import Reachability in your AppDelegate and just copy and past this code in your Appdelegate.m
- (id)init
{
self = [super init];
if (self != nil)
{
//[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
NSString *remoteHostName = #"www.google.com";
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[self.hostReachability startNotifier];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
self.wifiReachability = [Reachability reachabilityForLocalWiFi];
[self.wifiReachability startNotifier];
}
return self;
}
Add this method in your Common Class.
/*================================================================================================
Check Internet Rechability
=================================================================================================*/
+(BOOL)checkIfInternetIsAvailable
{
BOOL reachable = NO;
NetworkStatus netStatus = [APP_DELEGATE1.internetReachability currentReachabilityStatus];
if(netStatus == ReachableViaWWAN || netStatus == ReachableViaWiFi)
{
reachable = YES;
}
else
{
reachable = NO;
}
return reachable;
}
Note that APP_DELEGATE1 Is an instance of AppDelegate
/* AppDelegate object */
#define APP_DELEGATE1 ((AppDelegate*)[[UIApplication sharedApplication] delegate])
You can check internet connectivity anywhere in app using this method.
it's simple , you can use following method to check internet connection .
-(BOOL)IsConnectionAvailable
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Hope this helps you to network in Wifi mode only:
Utils.h
#import <Foundation/Foundation.h>
#interface Utils : NSObject
+(BOOL)isNetworkAvailable;
#end
utils.m
+ (BOOL)isNetworkAvailable
{
CFNetDiagnosticRef dReference;
dReference = CFNetDiagnosticCreateWithURL (NULL, (__bridge CFURLRef)[NSURL URLWithString:#"www.apple.com"]);
CFNetDiagnosticStatus status;
status = CFNetDiagnosticCopyNetworkStatusPassively (dReference, NULL);
CFRelease (dReference);
if ( status == kCFNetDiagnosticConnectionUp )
{
NSLog (#"Connection is Available");
return YES;
}
else
{
NSLog (#"Connection is down");
return NO;
}
}
//Now use this in required class
- (IBAction)MemberSubmitAction:(id)sender {
if([Utils isNetworkAvailable] ==YES){
NSlog(#"Network Connection available");
}
}
Try This to check internet connected or not
NSURL *url = [NSURL URLWithString:#"http://www.appleiphonecell.com/"];
NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url];
headRequest.HTTPMethod = #"HEAD";
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
defaultConfigObject.timeoutIntervalForResource = 10.0;
defaultConfigObject.requestCachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:nil delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:headRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (!error && response)
{
block([(NSHTTPURLResponse *)response statusCode] == 200);
}else{
block(FALSE);
}
}];
[dataTask resume];
Using Alamofire library:
let reachabilityManager = NetworkReachabilityManager()
let isReachable = reachabilityManager.isReachable
if (isReachable) {
//Has internet
}else{
//No internet
}
'Reachability' doesn't work since it won't detect if there is a response from the host or not. It will just check if the client can send out a packet to the host. So even if you are connected to a WiFi network and the WiFi's internet is down or the server is down, you will get a "YES" for reachability.
A better method is to try an HTTP request and verify the response.
Example below:
NSURL *pageToLoadUrl = [[NSURL alloc] initWithString:#"https://www.google.com/"];
NSMutableURLRequest *pageRequest = [NSMutableURLRequest requestWithURL:pageToLoadUrl];
[pageRequest setTimeoutInterval:2.0];
AFHTTPRequestOperation *pageOperation = [[AFHTTPRequestOperation alloc] initWithRequest:pageRequest];
AFRememberingSecurityPolicy *policy = [AFRememberingSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[policy setDelegate:self];
currentPageOperation.securityPolicy = policy;
if (self.ignoreSSLCertificate) {
NSLog(#"Warning - ignoring invalid certificates");
currentPageOperation.securityPolicy.allowInvalidCertificates = YES;
}
[pageOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
internetActive = YES;
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(#"Error:------>%#", [error description]);
internetActive = NO;
}];
[pageOperation start];
Only catch is that the "internetActive" gets updated with a delay upto the timeout mentioned in the above code. You can code inside the callback to act on the status.
Updated answer for Swift 4.0 & AlamoFire:
The answer I posted on Sept 18 is incorrect, it only detects if it is connected to network, not internet. Here is the correct solution using AlamoFire:
1) Create custom Reachability Observer class:
import Alamofire
class ReachabilityObserver {
fileprivate let reachabilityManager = NetworkReachabilityManager()
fileprivate var reachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus = .unknown
var isOnline: Bool {
if (reachabilityStatus == .unknown || reachabilityStatus == .notReachable){
return false
}else{
return true
}
}
static let sharedInstance = ReachabilityObserver()
fileprivate init () {
reachabilityManager?.listener = {
[weak self] status in
self?.reachabilityStatus = status
NotificationCenter.default.post(
name: NSNotification.Name(rawValue: ClickUpConstants.ReachabilityStateChanged),
object: nil)
}
reachabilityManager?.startListening()
}
}
2) Initialize on app start up
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
_ = ReachabilityObserver.sharedInstance
return true
}
3) Use this anywhere in your app to detect if online, such as in view did load, or when action occurs
if (ReachabilityObserver.sharedInstance.isOnline){
//User is online
}else{
//User is not online
}
Try this
check this link for Reachability file
Reachability
import this file in your .m and then write code
//This is to check internet connection
BOOL hasInternetConnection = [[Reachability reachabilityForInternetConnection] isReachable];
if (hasInternetConnection) {
// your code
}
Hope it helps.
Reachability* reachability = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == ReachableViaWWAN || remoteHostStatus == ReachableViaWiFi)
{
//my web-dependent code
}
else {
//there-is-no-connection warning
}
Related
I have added reachability into my project following things are working fine.
1.It Check successfully host request, wifi or mobile date active connection..
but i have tested the reachability of wifi with loss of internet connection , it may give the results like reachable via wifi...(example like you have active wifi connection but no internet received form wifi)
I did added NSTimers and achieved exact result, but i want to achieve the this thing by reachability so can anyone help to solve my issue...
i also had similar problem to detect if internet lost. example : you have connected to WIFI but internet is not active due to bill settlement or something.
If you are using AFNetworking there will be error code if you lost your internet connection while doing some POST request.
below is my code.
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:#"" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
//If response Sucess
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error %#",error);
if(error.code==-1009){
// This Error Comes when internet is not active.
so inside this you can make any action
}
}];
Here is my answer ,
I did not initialize timers just using completion blocks , but completion blocks with delay almost equal to NSTIMERS.
I have created api for reachability with all conditions..
just attach sample method to check the all conditions..
-(void) blocksWithReachabiltyCheck :(bool) r_Status
{
__weak id weakSelf = self;
callBack = ^{
double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
id strongSelf = weakSelf;
if (!strongSelf) {
return;
}
// Schedule the timer again
//callBack();
[weakSelf targetMethod:r_Status];
});
};
// Start the timer for the first time
callBack();
}
-(void)targetMethod:(bool)sender
{
NSString *remoteHostName = #"www.apple.com";
// NSString *remoteHostLabelFormatString = NSLocalizedString(#"Remote Host: %#", #"Remote host label format string");
// self.remoteHostLabel.text = [NSString stringWithFormat:remoteHostLabelFormatString, remoteHostName];
// NSLog(#"%#",remoteHostLabelFormatString);
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[self.hostReachability startNotifier];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
self.wifiReachability = [Reachability reachabilityForLocalWiFi];
[self.wifiReachability startNotifier];
if (sender == YES)
{
callBack();
}
}
//stop timer
-(void) RXStopNotifier
{
[self blocksWithReachabiltyCheck:NO];
}
//start notifier with host name
-(void) RXStartNotifier:(NSString *)hostNameString
{
[self blocksWithReachabiltyCheck:YES];
hostName = hostNameString;
}
#synthesize callBack;
#property (copy)__block void (^callBack)(void) ;
//Notification method
- (void) RXSeachabilityChanged:(NSNotification *)note
{
if (timerFlag == false)
{
timerFlag = true;
Reachability* curReach = [note object];
NetworkStatus netStatus = [curReach currentReachabilityStatus];;
statusReach = 0;
switch (netStatus)
{
case NotReachable: {
NSLog(#"Not Access ");
statusReach = 0;
break;
}
case ReachableViaWWAN:
// {
// NSLog(#"Reachable WWAN");
// statusReach = 1;
// // imageView.image = [UIImage imageNamed:#"WWAN5.png"];
// break;
// }
case ReachableViaWiFi: {
if (instantFlag == NO)
{
NSLog(#"Reachable WIFI or Reachable WWAN");
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:hostName] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:1];
NSURLResponse *response = nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//NSLog(#"response %d", [(NSHTTPURLResponse *)response statusCode]);
if ([(NSHTTPURLResponse *)response statusCode] == 200) {
statusReach = 1;
NSLog(#"Success");
}
else
{
statusReach = 2;
NSLog(#"Failed");
}
}
else
{
statusReach = 1;
}
break;
}
}
}
}
if any one having doubts just reach me....
We are developing a module, which checks whether the Internet connection either is active or not active or disabled. The previous scenarios work well, except the case if there is a WI-FI connection but the internet is not active. We have made sure that the data mobile connection (3G, 4G) is not enabled.
We have seen that everybody recommends using the Tony Million's Reachability class, which replaces the Apple's one.
Below, there is the code that we are using:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Reachability *reachability = [Reachability reachabilityWithHostname:#"www.google.com"]; //Cambiar por el servidor
reachability.reachableBlock = ^(Reachability *reachability) {
NSLog(#"Network is reachable.");
};
reachability.unreachableBlock = ^(Reachability *reachability) {
NSLog(#"Network is unreachable.");
};
// Start Monitoring
[reachability startNotifier];
return YES;
}
Whether there is a WI-FI connection but the internet is not active, it does not work because it goes to:
reachability.reachableBlock = ^(Reachability *reachability) {
NSLog(#"Network is reachable.");
}
instead of
reachability.unreachableBlock = ^(Reachability *reachability) {
NSLog(#"Network is unreachable.");
}
What we are missing?
Thank you in advance,
Regards
Try this one,
- (BOOL)isNetAvailable
{
BOOL isNetAvailable = NO;
Reachability *reach = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus != NotReachable)
{
isNetAvailable = YES;
}
else
{
isNetAvailable = NO;
}
return isNetAvailable;
}
You can use :
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:[NSURL URLWithString: #"http://www.google.com"]];
NSHTTPURLResponse *response = nil;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:nsrequest returningResponse:&response error:&error];
if(response.statusCode==0){
NSLog(#"Network is unreachable.");
}
else{
NSLog(#"Network is reachable.");
}
I am using AFNetworking in my App for communicating with server. How can i check whether my host(server) is available or not, i should check this before i am sending a request to server.
Using AFNetworking 2.0 that provide method like below you can check instead of using Reachability class.
- (void)viewDidLoad {
[super viewDidLoad];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
}
After set start-monitoring in to viewDidLoad you can check any where in to class using Bellow Method.
if ([[AFNetworkReachabilityManager sharedManager] isReachable])
{
NSLog(#"IS REACHABILE");
}
else
{
NSLog(#"NOT REACHABLE");
}
You can try the following code:
Reachability *reach = [Reachability reachabilityWithHostname:#"google.com"];
if ([reach isReachable]) {
// Reachable
if ([reach isReachableViaWiFi]) {
// On WiFi
}
} else {
// Isn't reachable
[reach setReachableBlock:^(Reachability *reachblock)
{
// Now reachable
}];
[reach setUnreachableBlock:^(Reachability*reach)
{
// Now unreachable
}];
}
OR
You can do like this:
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://google.com"]];
[client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (status == AFNetworkReachabilityStatusNotReachable) {
// Not reachable
} else {
// Reachable
}
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
// On wifi
}
}];
NSOperationQueue *operationQueue = self.operationQueue;
// This block is automatically invoked every time the network status changes
[self.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
// we need to notify a delegete when internet connection is lost.
// use this delegate to notify the user.
//[delegate internetConnectionLost];
NSLog(#"No Internet Connection");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(#"Host available through WIFI");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(#"Host available through 3G");
break;
default:
NSLog(#"Unkown network status");
[operationQueue setSuspended:YES];
break;
}
}];
[self.reachabilityManager startMonitoring];
I have a Boolean value to check Reachability with following code.
#property (nonatomic,assign) BOOL isOnline;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
Reachability * reach = [Reachability reachabilityWithHostname:#"www.google.com"];
reach.reachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"Reachable");
self.isOnline = YES;
});
};
reach.unreachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
self.isOnline = NO;
NSLog(#"Unable");
});
};
[reach startNotifier];
However when i check it with
if(self.isOnline == YES)
{
NSLog(#"YES");
}
else{
NSLog(#"NO");
}
It's only showing NO. I am sure Reachability can reach to the internet and i have connection. However Boolean value is not set to YES.
How can i do it?
It seems that you are using the Reachability by Tony Million so 1st thing i would suggest is to use isReachable function in it.
Secondly, there is an issue with this reachability in iOS7 so to resolve this you have to do the following
In the Function -(BOOL)isReachableWithFlags:(SCNetworkReachabilityFlags)flags in Reachability.m you have to commit one line
#if TARGET_OS_IPHONE
if(flags & kSCNetworkReachabilityFlagsIsWWAN)
{
// We're on 3G.
if(!self.reachableOnWWAN)
{
// We don't want to connect when on 3G.
// Comment this line
//connectionUP = NO;
}
}
#endif
How can I check whether the app is connected to the internet or not?
currently, I am using this code in my appdelegate.m file
dispatch_queue_t connectivityThread = dispatch_queue_create("com.gm.kart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
if([GMMConnectivity hasConnectivity])
NSLog(#"%#", #"connected");
else
NSLog(#"Not connected");
usleep(10000000);
}
});
and when I click my login button I want to do a check whether the internet is connected or not using NSnotificationcenter?
Please help me
After download bellow example.
http://developer.apple.com/iphone/library/samplecode/Reachability/index.html
you can use it in your Project like bellow steps:-
included Apple's Reachability.h & .m from their Reachability example.
add the SystemConfiguration framework.
put bellow method in to your appdelegare.m file:-
- (BOOL) connectedToNetwork{
Reachability* reachability = [Reachability reachabilityWithHostName:#"google.com"];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable)
{
isInternet =NO;
}
else if (remoteHostStatus == ReachableViaWWAN)
{
isInternet = TRUE;
}
else if (remoteHostStatus == ReachableViaWiFi)
{ isInternet = TRUE;
}
return isInternet;
}
isInternet is a BOOL declear in to your .h class
as per your code:-
dispatch_queue_t connectivityThread = dispatch_queue_create("com.GMM.assamkart.connectivity", NULL);
dispatch_async(connectivityThread, ^{
while (true){
isInternet =[self connectedToNetwork];
if (isInternet)
{
NSLog(#"connected");
}
else
{
NSLog(#"Not connected");
}
// usleep(10000000);
}
});
-(BOOL) connectedToInternet
{
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"]];
return ( URLString != NULL ) ? YES : NO;
}