Actual network connection status detection under iOS
Preface
Network connectivity Status detection is a very common requirement for our iOS app development. For a better user experience, we will show local or cached content when there is no network, and prompt the user appropriately. For the vast majority of iOS developers, the various reachablity frameworks that have changed from Apple's sample code are a common choice for achieving this requirement, such as this library. But in fact, all implementations based on this scenario will not help us detect the true network connection state, they can detect only the local connection state, this situation includes but is not limited to the following scenario:
1. Now popular public WiFi, requires web authentication, authentication before the Internet, but the local connection has been established;
2. There is a local network connection, but the signal is poor, the actual inability to connect to the server;
The 3.iOS connected routing device itself is not connected to the extranet.
Cocoachina has been a lot of netizens to ask questions and spit groove, such as:
Apple's reachability example tells us that its capabilities are limited by:
"Reachability cannot tell your application if you can connect to a particular host, only that's interface is available th At might allow a connection, and whether this interface is the WWAN. "
Apple's Scnetworkreachability API tells us more: "Reachability does not guarantee, the data packet will actually is received by The host. "
Reachability-related frameworks are based on the scnetworkreachability to achieve network detection, so the actual network connection can not be detected.
In view of this, I hope to create a general, simple, reliable real network connection status detection framework, so realreachability was born.
Realreachability Brief Introduction
Realreachability is the open source library that I posted to GitHub 1 months ago, and the project address is as follows: Https://github.com/dustturtle/RealReachability. In just 1 months the harvest of more than 100 star, which mainly from foreign friends, recently also on GitHub's OC plate trend rankings, I am quite pleasantly surprised. The development of this framework is based on the actual needs of the project, offline mode of network connection status requirements are more stringent, and the actual scenario will often encounter "pseudo-connection" situation, reachability face this scenario is powerless. After a multi-party study, ping capability was introduced (this scheme has the lowest traffic cost and the simplest), which realizes simple real-world network connection monitoring, and after refining and optimization, this framework is available. It can be told that the framework has been tested in AppStore applications and has been tested for a long time and can be used with confidence.
Introduction to integration and use
The simplest method of integration is Pod:pod ' realreachability '.
Manual integration: Add the Realreachability folder to the project.
Dependency: xcode5.0+, Support Arc, ios6+. The project needs to introduce Systemconfiguration.framework.
Its interface design and invocation method and reachability very similar, we can be seamless, very convenient. Turn on network monitoring:
[GLobalRealReachability startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
Callback code example:
- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];
NSLog(@
"currentStatus:%@"
,@(status));
}
Example of triggering real-time network status query code:
[GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
switch
(status)
{
case
NotReachable:
{
// case NotReachable handler
break
;
}
case
ReachableViaWiFi:
{
// case ReachableViaWiFi handler
break
;
}
case
ReachableViaWWAN:
{
// case ReachableViaWWAN handler
break
;
}
default
:
break
;
}
}];
To query the current actual network connection status:
Reachabilitystatus status = [reachability currentreachabilitystatus];
Demo:
We have included a simple demo project in GitHub's repository, which can be downloaded and run directly. Related API calls can also refer to the implementation in the demo. Demo
The realization principle of realreachability
Realreachability Frame Composition:
Realreachability architecture schematic diagram realreachability consists of 3 main modules: Connection, Ping, FSM, wherein the ping module is encapsulated by a ping sample code that is also provided by Apple, The connection module realizes the local state detection based on scnetworkreachability, and the FSM module is a finite state machine. Through the FSM State Management control connection module and Ping module work together, and through the configurable timing strategy and other business logic optimization, and finally got our implementation.
PS: Wherein the connection module and the Ping module can also be used independently, providing local network detection and ping capabilities, interested readers can also try (call way please refer to realreachability Open source code).
Conclusion
Hopefully this framework will help you with iOS development! You can contact me if you have any questions or problems with your use.
Li Hongqiang Actual network connection status detection under iOS