What I am trying to achieve, is monitor any internet connectivity? So the phone needs an active connection to the internet. If it does display a UIAlertView with the option to Try Again (try the connection again to see if it has changed).
I am trying to use Reachability and connection to the api.parse.com link.
In my AppDelegate I call the setup of Reachability like this:
// Use Reachability to monitor connectivity
[self monitorReachability];
The monitorReachability is setup like this:
- (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];
}
I also have the reachability changed method as follows:
EDIT - updated method
- (void)reachabilityChanged:(NSNotification* )note {
Reachability *curReach = (Reachability *)[note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NSLog(#"Reachability changed: %#", curReach);
networkStatus = [curReach currentReachabilityStatus];
if (networkStatus == NotReachable) {
NSLog(#"NOT REACHABLE");
return;
} else {
NSLog(#"REACHABLE");
}
What I am trying to understand is the responses back. From the above it looks like I have a pointer to the current status and I am not sure how to use this. Basically I want an if statement to check if that link is reachable via the internet connection, if its not I can through an AlertView. I can then setup a boolean for the UIAlertView to use i.e. showingConnectionAlert, which can then be brought down when the connection is changed and picked up. I am unsure where to put this too.
One of the simplest ways to use the Reachability class is to import the Reachability.h into your rootViewController or whatever one is going to need the connection, then simply run this code...
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
NSLog(#"No internet connection!");
//Alert View in here
}
else {
//Do something in here with the connecion e.g:
[self performSelector:#selector(startNSURLRequest) withObject:nil afterDelay:30.0];
}
That should simplify the process a bit.. let me know how you get on. T
Related
In my app i am trying to get network status using NetworkReachability. I tried it when i am under wifi or 3g connection and it works perfectly.
The problem is when i am connected to a personal hotspot/tethering(tested with iPhone to iPhone, Android to iPhone and routers with sim) i get always 3g connection and not wifi.Tested with ios10. Is there any way without using private framework to control if i am under tethering connection or hotspot?
You have check Reachability for internet, Using following link to download Reachability demo project from Apple:
https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html
,and add both .h and .m files to your project.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
- (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"); }
}
In GitHub, AFNetworking library provide the check the network Reachability status. Using the Network Reachability Manager object file to detect the status of internet reachability status.
Shared Network Reachability
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(#"Reachability: %#", AFStringFromNetworkReachabilityStatus(status));
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
As per the title, I've turned off Wifi on my iPhone and it has an LTE connection, yet when I call Reachability:currentReachabilityStatus it returns ReachableViaWifi. Code is:
in the init:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChange:)
name:kReachabilityChangedNotification
object:nil];
_reachability = [Reachability reachabilityWithHostName:[[MYURLs defaultServerBaseURL] host]];
[_reachability startNotifier];
NetworkStatus status = [_reachability currentReachabilityStatus];
// here status is NotReachable
....
- (void) reachabilityChange: (NSNotification*) notification
{
NetworkStatus status = [self.reachability currentReachabilityStatus];
// here status is ReachableViaWifi
}
Why is the status ReachableViaWifi when Wifi is disabled?
I'm turning the wifi off before running the code.
In reachabilityChange:, instead of calling [self.reachability currentReachabilityStatus], look at the object of the notification.
- (void) reachabilityChange: (NSNotification*) notification {
Reachability *reach = [notification object];
NetworkStatus status = [reach currentReachabilityStatus];
}
Seems like this is a bug in iOS8 Beta as the same code produces expected results in iOS7.
I just ran into this same issue. Grabbing the latest Reachability classes from Apple fixed it, though.
Reachability
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.
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..
}
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
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
}
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?
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);
}
I have create a method to check if the host is reachable. I had download the Reachability class (both .h & .m) from apple developer websites and import to my project. I have pass the NSString Name as the URL (host Name). The hostName is http://www.google.com. However, no matter what host name I pass to this method and its always return NO (connectToHost). The code as below:
- (BOOL) checkHostAvailability:(NSString *)Name{
BOOL connectToHost;
hostReach = [[Reachability reachabilityWithHostName:Name] retain];
[hostReach startNotifier];
NetworkStatus hostStatus = [hostReach currentReachabilityStatus];
NSLog(#"Name: %#", Name);
NSLog(#"hostStatus is %#", hostStatus);
if(hostStatus == NotReachable){
NSLog(#"Here is the checkHostAvailability Method and host NOT reachable");
connectToHost = NO;
}else{
NSLog(#"Here is the checkHostAvailability Method and host is reachable");
connectToHost = YES;
}
return connectToHost;
}
After a few hours of investigation, I have found out that the NetworkStatus hostStatus always equal to null. I assume this is why this method is not working. And I have spend 8 hours to find out the problem with this code and search out this website, however I still couldn't find the problem and solution.
Please help and much appreciated.
Remove the 'http://' from the host name.
If you wanted to use http://www.google.com as your host, you would pass 'google.com' as the hostname. Do not include http://, the ending slash or anything that could follow an ending slash. www. is fine to include.
[Reachability reachabilityWithHostName:#"google.com"];
This is the code I use for all the connection checks (see here: iOS Developer Library -Reachability) and it is working fine for me :
-(BOOL) hasConnection{
//check network status _ iphone settings
Reachability *internetReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];
NetworkStatus status=[internetReachability currentReachabilityStatus];
if (status == NotReachable) {
NSLog(#"No internet");
//[internetReachability stopNotifier];
return NO;
}else {
NSLog(#"Internet is ON");
// [internetReachability stopNotifier];
//check internet connection _reachable path
Reachability *hostReachable=[Reachability reachabilityWithHostName:#"www.apple.com"];
BOOL connectionRequired =[hostReachable connectionRequired];
NSLog(#"%hhd",connectionRequired);
[hostReachable startNotifier];
Reachability *wifiReachability=[Reachability reachabilityForLocalWiFi];
[wifiReachability startNotifier];
NetworkStatus hostStatus=[hostReachable currentReachabilityStatus];
if(hostStatus == NotReachable){
NSLog(#"No internet connection");
[hostReachable stopNotifier];
return NO;
}else{
NSLog(#"Connection is ok");
[hostReachable stopNotifier];
return YES;
}
}
}