Is there an event to disconnect from the Internet in iOS?

I have an application that requires an internet connection for a specific library that I am using (XIMSS for communication). It receives status updates from the server, and I cannot change the source of lib. Is there any way to detect that the device is disconnected from the Internet? I would like you to not have a ping server every x seconds to see if there is still a connection.

+4
source share
4 answers

Apple Reachability does it for you. Tells you about your connection changes.

+8
source

On iOS, you can check this: -

-(BOOL)returnInternetConnectionStatus{ ReachabilityLattest *reach = [ReachabilityLattest reachabilityForInternetConnection]; NetworkStatus internetStatus = [reach currentReachabilityStatus]; if ((internetStatus != NotReachable)) { return TRUE; } else { return FALSE; } } 

For more information, see Sample Code for Better Performance

Hope this helps you!

+3
source

Please note that all pointers to Reachability are a good start, and you definitely need the opportunity to upgrade, but for a messaging application this is usually not enough. Reach never sends packets. It just tells you if the device can even try to send a packet if you ask for it. So it basically tells you that you have an IP address and you know the gateway. It does not tell you that you can contact the server you care about, and it does not tell you at all whether this server can contact you (which is a very important issue for messaging applications).

In most cases, if Reach was sufficient, you won't need Reach. Apple strongly recommends (and not in vain) that you do not check Reach before sending packages. Just send the packets and handle the error if it appears. But this does not help applications that should receive data at arbitrary points in time.

If the connection is poor (especially due to poor cell coverage), or if there is a firewall between you and the server, you can easily get positive results from Reachability, even if you no longer receive messages from the server. The only way to detect this situation is to send a packet and receive a packet (ie "Ping").

Reachability is also insufficient to notice when your connection changes. For example, if you change IP addresses (quite often when you are driving around town), Reachability will not always tell you (maybe if you use SCNetworkReachabilityCreateWithAddressPair() , this has been a while since I worked on this problem; example code Reachability doesn't work that way anyway).

So, Reachability is a good first start, but at the end of the day you still need a heartbeat if you want to discover that the server is no longer talking to you. The main thing to remember is that there is no such thing as a β€œconnection” in IP. There are only packages. You can send them, and you can receive them. But if you do not accept them, there is no way to distinguish "no one sends them" from "they do not arrive." The TCP illusion gives "connections" only when packets are exchanged.

(By the way, working with a network connection in a messaging app is probably the most complicated iOS code I've ever worked with. Testing is a nightmare. I used my phone in the refrigerator to disable its network access. Now there is network air conditioning ...)

+3
source
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReachablilityChange:) name:kReachabilityChangedNotification object:nil]; 

Add a notifier for reachability change notifications. This will cause reachability value. Change when changing network. In ReachablilityChange Function Discovery

  NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 

Get status in hostStatus

  if(hostStatus == NotReachable) 

{Make your code here;}

0
source

Source: https://habr.com/ru/post/1496791/


All Articles