Reachability method (startnotifier) is not calling - iphone

Here is my reference code. I have put break point in startnotifier method but it is not being called.
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
I have written this part of code in appdelegate.m (didFinishLaunchingWithOptions).
I have declared var in appdelegate.h like below....
#interface AppDelegate : UIResponder < UIApplicationDelegate >
{
Reachability *internetReach;
Reachability *wifiReach;
Reachability *hostReach;
}
Why breakpoint in startnotifier is not being called and hence nsnotification is not calling observer function if I change the network.

+(BOOL)ConnectedToNetWork
{
Reachability *HostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [HostReach currentReachabilityStatus];
bool result = false;
if (internetStatus == ReachableViaWiFi)
result = true;
else if(internetStatus==ReachableViaWWAN)
result = true;
return result;
}
use this method whre ever u want to check connection

Related

[NSConcreteNotification isReachable]: unrecognized selector sent to instance

I'm trying to get ASIReachability to work in my app which works when the connection isn't there but when the connection exists it give the following error:
2013-04-08 12:26:20.501 Your Llanelli Companion[1576:207] -[NSConcreteNotification isReachable]: unrecognized selector sent to instance 0x7d84d30
I can't seem to remedy this and it's beginning to bug me.
.m file:
- (void) CheckIfAInternetConnectionExists
{
Reachability *reach = [[Reachability reachabilityWithHostName:#"http://176.31.101.181:8020/listen.pls"]retain ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[reach startNotifier];
}
- (void) reachabilityChanged:(Reachability *)reach {
if ([reach isReachable ]) {
NSLog(#"connection");
ServerFound = YES;
[self PrepareStream];
} else{
NSLog(#"no connection");
ServerFound = NO;
[self PrepareStream];
}
}
If someone can figure this out for me I'd be very grateful.
When you receive the notification, the parameter to your reachabilityChanged: method will be the notification itself. To get the Reachability object, you'll need to get it from the notification by sending -[NSNotification object] to the notification.
So something like this should work:
- (void) reachabilityChanged:(NSNotification *)note {
Reachability *reach = [note object];
if ([reach isReachable ]) {
NSLog(#"connection");
ServerFound = YES;
} else{
NSLog(#"no connection");
ServerFound = NO;
}
[self PrepareStream];
}

Which is the simplest way to check for Internet connection in iOS? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check for internet connection - iOS SDK
I'm searching the fastest and simplest way for check the connection in iOS.
I've found this:
-(BOOL)connectedToNetwork {
NSURL* url = [[NSURL alloc] initWithString:#"http://google.com/"];
NSData* data = [NSData dataWithContentsOfURL:url];
if (data != nil)
return YES;
return NO;
}
Do you know if is there something even simpler???
Guys, thanks for all the answers, but I'm searching for the simplest, lightest, solution, not for the best one (i.e. distinction between 3G/Wi-Fi is not needed, I'm only searching a YES/NO reach for a website)
Take a look at the Reachability Example provided by Apple.
The problem your approach may have is that you could have a timeout and thus, the synchronized download of some data may block your app. As a result Apple may reject your app.
The Reachability Example can be used as follows:
Reachability *_reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus remoteHostStatus = [_reachability currentReachabilityStatus];
if (remoteHostStatus == NotReachable) {
// not reachable
} else if (remoteHostStatus == ReachableViaWiFi) {
// reachable via Wifi
} else if (remoteHostStatus == ReachableViaWWAN) {
// reachable via WWAN
}
I suggest dont go with this approach. I faced a rejection of one of my app because of this code. Instead go with Apple's Reachability Classes.
Use this code to check whether the device is connected to internet or not
use this code in viewDidLoad :
Reachability* internetReachable; = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
hostReachable = [Reachability reachabilityWithHostName: #"www.apple.com"] ;
[hostReachable startNotifier];
and add this function to your code:
-(void) checkNetworkStatus:(NSNotification *)notice
{
recheabilityBool=FALSE;
nonrecheabilityBool=FALSE;
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
nonrecheabilityBool=TRUE;
internetCon=0;
//NSLog(#"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:#"conKey"];
//NSLog(#"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(#"The internet is working via WWAN.");
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
internetCon=0;
if( nonrecheabilityBool==FALSE)
{
//NSLog(#"A gateway to the host server is down.");
}
break;
}
case ReachableViaWiFi:
{
if(recheabilityBool==FALSE)
{
recheabilityBool=TRUE;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
internetCon=404;
[prefs setInteger:internetCon forKey:#"conKey"];
//NSLog(#"The internet is working via WIFI.");
break;
}
//NSLog(#"A gateway to the host server is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
//NSLog(#"A gateway to the host server is working via WWAN.");
break;
}
}
}
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:#"www.example.com"]; // set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
How to check for an active Internet connection on iOS or OSX?
As #who9vy said use Reachability Example
Import the Two classes Reachability.h and Reachability.m into your project
Use method to check the Internet Connection
- (BOOL)isConnectedToInternet
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
The best way to check reachability is Apple Rechability class
Check this link
Hope it helps you..

Checking Network Availability

I use reacheability class for checking my network connectivity in my app...
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus==NotReachable)
{
NSLog(#"NR");
}
I need to find when the network status change (i.e when the network status changes from reachable to notreachable and vice versa).
Is there any delegates to find this thinks, Any suggestions ?
I suggest to make use of Apple's Reachability class. Here is a sample App by Apple.
and also check this links.
http://www.switchonthecode.com/tutorials/iphone-snippet-detecting-network-status
Used this flag kReachabilityChangedNotification to find the change in network status and passed that to a NSNotificationCenter
Here is the code:
NSString *host = #"https://www.apple.com"; // Put your host here
// Set up host reach property
hostReach = [Reachability reachabilityWithHostname:host];
// Enable the status notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
[hostReach startNotifier];
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability *reachability = [note object];
NSParameterAssert([reachability isKindOfClass:[Reachability class]]);
if (reachability == hostReach) {
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus==NotReachable)
{
NSLog(#"notreacheable");
}
else {
NSLog(#"reacheable");
[[NSNotificationCenter defaultCenter]postNotificationName:#"startUpdatingTable" object:nil];
}
}
}
use Reachability" class
add flooding method in app delegate so you can use this method any ware in project
#import "Reachability.h"
-(BOOL)isHostAvailable
{
//return NO; // force for offline testing
Reachability *hostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
return !(netStatus == NotReachable);
}

Internet checking ios application

I have created an iPhone application Where i want to check internet connectivity.At the didFinishLaunchingWithOptions method of the app delegate method i wrote
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" title:firstTabTitleGlobal bundle:nil];
viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" title:secondTabTitleGlobal bundle:nil];
newNavController = [[UINavigationController alloc] initWithRootViewController:viewController1];
userNavController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:newNavController,userNavController,nil]
Reachability *r = [Reachability reachabilityWithHostName:globalHostName];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
[self showAlert:globalNetAlertTitle msg:globalNetAlertMsg];
[activityIndicator stopAnimating];
}
else
{
[activityIndicator stopAnimating];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
}
My Code is ok because when no internet connectivity then show alert.But problem is when no interner then default.png is shown. When i run apps again then the apps runs from the showing default.png. And nothing happen.
Thanks in advance.
application:didFinishLaunchingWithOptions: will only run when an app is launched.
If you want your application to check for availability on subsequent application activation, try putting your code in applicationDidBecomeActive:
What might be better is using NSNotifications, to dynamically tell you if you have connectivity. You can do this with an apple class called 'reachabilty'. Once you have included the file in your project, you can then use something like this;
//in viewDidOnload
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleNetworkChange:)
name:kReachabilityChangedNotification object:nil];
reachability = [[Reachability reachabilityForInternetConnection] retain];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if (status == NotReachable) {
//Do something offline
} else {
//Do sometihng on line
}
- (void)handleNetworkChange:(NSNotification *)notice{
NetworkStatus status = [reachability currentReachabilityStatus];
if (status == NotReachable) {
//Show offline image
} else {
//Hide offline image
}
}
(this is the corrected code from Reachability network change event not firing)
You will then be able to update you images as soon as any network change takes place. However don't forget to remove yourself from receiving notifications in the dealloc with;
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
If you need any more information about how to implement this, I would be happy to help!
Jonathan
This works great in Swift 5. Returns a Boolean. It also works on mobile data.
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
}
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
/* Only Working for WIFI
let isReachable = flags == .reachable
let needsConnection = flags == .connectionRequired
return isReachable && !needsConnection
*/
// Working for Cellular and WIFI
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
let ret = (isReachable && !needsConnection)
return ret
}
}
hope this helps.

iphone:How to check each & every time internet connection in iphone?

Hello friends,
I want to check internet connection each & every time is active or not in my iPhone apps so please tell me any link or any idea to develop this functionality.
Thanks in advance..
Use Reachability Classes
+ (BOOL)isNetworkAvailable {
Reachability *internetReach;
internetReach = [Reachability reachabilityForInternetConnection];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
if(netStatus == NotReachable) {
NSLog(#"Network Unavailable");
return NO;
}
else
return YES;
}
//check for internet connection
NSStringEncoding enc;
NSError *error;
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.apple.com"] usedEncoding:&enc error:&error];
if (connected == nil) {
//no internet connection..display alert
} else {
//call your function
}
This should help you.
You can use the Reachability example from apple developer.
I use it like this:
-(BOOL) isInternetReachable
{
Reachability *r = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if(internetStatus == NotReachable)
{
return NO;
}
return YES;
}
You can also check to see if a certain server is up with the provided methods.
With the help of Reachability Class and also go through...