Iphone Internet Connection Verification - Reachability - iphone

I read there were many problem with the Reachability sample app that apple provided, but now with their new 2.2 implementation it's suppose to work fine.
So I just want an advice before I integrate it into my app instead of my current implementation.
Basically I'm asking 2 question:
Will this code work ? I need to know if internet is available through 3G or WIFI. (If there is a WIFI with no internet, of course I need it to return false)
2.I'm planning to deploy my app on ios 3.0 and above, do I need to perform any adjustments so it won't crash on ios below 4.0 (cause Reachability 2.2 said to be working on ios 4) ?
Thanks alot!!
+ (BOOL) isInternetReachable
{
NetworkStatus status = [[Util sharedReachability] currentReachabilityStatus];
return (status != NotReachable);
}
+ (Reachability*) sharedReachability
{
if (reachability == nil)
{
reachability = [[Reachability reachabilityForInternetConnection] retain];
[reachability startNotifier];
}
return reachability;
}

Yes this code is gonna work. It is right.

Related

How to identify programmatically which connection is active (WiFi or Ethernet) in Iphone

I have a requirement to identify, which connection is active (WiFi or Ethernet) in Iphone programmatically. If, user is using WiFi then I have to display different view controllers in my app.
Please help.
you can use apple provided Reachability class hear bellow example please check this sample code who provide by apple.
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.
when u use it you just called Bellow method:-
Reachability* wifiReach = [[Reachability reachabilityWithHostName: #"www.apple.com"] retain];
NetworkStatus remoteHostStatus = [wifiReach currentReachabilityStatus];
switch (remoteHostStatus)
{
case NotReachable:
{
NSLog(#"Access Not Available");
break;
}
case ReachableViaWWAN:
{
NSLog(#"Reachable WWAN");
break;
}
case ReachableViaWiFi:
{
NSLog(#"Reachable WiFi");
break;
}
}

is Reachability is update in realtime (reachabilityWithHostName)? [duplicate]

i read the https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
my Question is if the www.apple.com is become not active Suddenly..this code will alert me?
or just if my connection fail's?
That's absolutely right.
It will fire off a kReachabilityChangedNotification notification that tells you the new reachability state.
You get the new reachability state something like this :
- (void)reachabilityChanged:(NSNotification *)notification {
Reachability *reachability = notification.object;
if (NotReachable == reachability.currentReachabilityStatus)
NSLog(#"No longer reachable");
}

Reachability status message appears only once

I am popping a message alerting the user that he/she has lost WIFI/intenet connection. For that, i followed the Reachability example given by apple.
I added the 2 reachability classes, Rechability.h and .m, and i added the codes given in its app delegate to mine as well (an exact replica). This works perfectly.
My problem is that, this message appears only once, i want it to be displayed when it goes to each and every view.
All codes that i am using can be found here. Help
Mmm... Not sure to understand what you expect Rechability does.
This class is designed to get any change in your rechability status. When a change is detected Reachability send a notification, but if nothing changes you won't get any notification.
EDIT: to get your reachability status and to use it later you can add a BOOL (internetIsDown) to the method where you read notification from Reachability.
- (void)checkNetworkStatus:(NSNotification *)notice {
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus) {
case NotReachable: {
internetIsDown = YES;
break;
} case ReachableViaWiFi: {
internetIsDown = NO;
break;
} case ReachableViaWWAN: {
internetIsDown = NO;
break;
}
}
}
Now you can check for this BOOL value whenever needed and display an alert to the user.
N.B. internetIsDown should be a singleton if you want to access its value from any viewController!!!
I have an application where I have tabs. The root controller registers for reachability messages. An UIAlertView is used to display a warning. This is shown in all parts of the application.
You could call [Reachability reachabilityForInternetConnection] in each view's viewDidAppear method...
But like others have mentioned, it could be kind of annoying to see the same message over and over.

iPhone app, alert users when they aren't connected to the internet?

What's an easy way to code an alert that warns users when they aren't connected to the Internet? I'm using Xcode, right now when there is no connection it is just a white screen in uiwebview.
Here you can check if wifi has connection, 3g,or none atall:
if ([[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWiFi) {
// Do something that requires wifi
} else if ([[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWWAN) {
// Do something that doesnt require wifi
} else if ([[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == NotReachable) {
// Show alert because no wifi or 3g is available..
}
Apple provides all the necessary Reachability api/source here: Reachability Reference
I have made these custom convenience functions for myself in all my projects:
+ (BOOL)getConnectivity {
return [[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] != NotReachable;
}
+ (BOOL)getConnectivityViaWiFiNetwork {
return [[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWiFi;
}
+ (BOOL)getConnectivityViaCarrierDataNetwork {
return [[Reachability reachabilityWithHostName:#"google.com"] currentReachabilityStatus] == ReachableViaWWAN;
}
And used like so:
if ([ServerSupport getConnectivity]) {
// do something that requires internet...
else {
// display an alert
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Network Unavailable"
message:#"App content may be limited without a network connection!"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease];
[alert show];
}
If your application requires persistent internet connection, you can include the key UIRequiresPersistentWiFi in your plist file. Using this, iOS will automatically display an alert to the user in the airport mode.
Also, iOS will ensure that the wifi radio isn't switched off/hibernated while your app is in the foreground.
Note that internet connectivity is very dynamic on a mobile device (multiple radios, multiple access points, multiple cell towers, travel, competing radio interference, "your holding it wrong", etc.), and thus Reachability is not a perfect solution, as the net connectivity can very often can change just before, during or after Reachability performs its checks. Reachability can outright lie (yes we're connected to an access point, true even if broadband is dead on the other side of the access point). It's also quite common for Reachability to report no connectivity, just as its own request turns the radios on to get a great connection a couple seconds later.
Best thing to do is to just try to access data from the network, spin an activity indicator while waiting, and offer the user some API element to give up if they end up waiting too long, in their opinion, not some developer's opinion... They know better than you do how long it takes for a (re)connection to come up in their area, and how long they are willing to wait.

how to implement Network Reachebility status in multiple files?

I am using readability class to check network availability and it works fine with this code. But in my app I am having approx 25 view that needs to check the network. I need to know that do I have to write pieces of code in every file ? or is there any way to write it once ?
In the code there is 3 methods that I have to implement to check the network status.
any good suggestions ?
Thanks...
You can use this sample application for your requirement. Provided by APPLE.
In this they have kept it under Application Delegate to be available to all the classes.
Hope it helps.
here goes, import the header in the app delegate file.
in applicationDidFinishLaunching add
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(reachabilityChanged:)
name: kReachabilityChangedNotification object: nil];
this means the appDelegate will be informed each time the reachability is changed. you need to implement the following:
add
-(void)reachabilityChanged: (NSNotification* )note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(curReach == hostReach)
{
if (netStatus == NotReachable)
{
//no conn
}
else if (netStatus !=NotReachable)
{
//has conn
}
}
}
this is just an overview and you wont learn by just copying and pasting right, the reachability class can give you all you want to know, even they type of connection... so keep playing.
Make a static method in Reachability class like
-(BOOL)isInternetReachable{
//your code to check the internet connectivity
// return yes or no
}
the you can use as simple as
if([Reachability isInternetReachable])
{
///// do ur stuff
}
else{
////show some error message
}