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:
How can I tell if my device is really connected to the Internet? Instead of just a network connection
[reachability Reachabilitywithhostname:] totally useless!
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 that the data packet would actually be received by the host."
All frameworks related to reachability are detected by scnetworkreachability in the underlying implementation, so the actual network connection condition cannot 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 author 2 months ago released to GitHub Open Source Library, there are more than 1000 star,200 multiple fork, after several modifications, the current pod version is 1.1.2.
The project address is as follows:
Https://github.com/dustturtle/RealReachability.
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 the AppStore shelves, and is constantly improving, the pursuit of stable friends can use the latest pod version (fixed most of the known problems, refer to the use of the demo).
Integration and Usage Introduction integration
- 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.
Introduction to use
Its interface design and invocation method and reachability very similar, we can be seamless, very convenient.
Turn on network monitoring (recommended to listen in didfinishlaunchingwithoptions):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [GLobalRealReachability startNotifier]; returnYES;}
Monitoring network change notifications:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(networkChanged:) name:kRealReachabilityChangedNotification object:nil];
Example of a notification callback code:
- (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:^(ReachabilitystatusStatus) {switch (status) { Case notreachable:{// Case notreachableHandler Break; } Case Reachableviawifi:{// Case ReachableviawifiHandler Break; } Case Reachableviawwan:{// Case ReachableviawwanHandler Break; }Default: Break; } }];
To query the current actual network connection status:
ReachabilityStatus status = [reachability currentReachabilityStatus];
To set the host server address for ping detection (optional):
Note: Here you need to make sure that the server supports the ping operation. When not set, we use baidu.com as the ping server by default.
You can use your own server, or use a stable, reliable network address (such as Baidu, QQ, etc.). The setup example is as follows:
GLobalRealReachability.hostForPing = @"www.baidu.com";
Gets the current data network connection type (Advanced feature):
WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
This type can be used to optimize the experience of the application, such as setting different network time-outs under different network types, and in this respect, you can refer to Ctrip's share of the optimization scheme: http://www.infoq.com/cn/articles/ How-ctrip-improves-app-networking-performance
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
Realreachability principle of implementation realreachability frame composition:
Realreachability mainly consists of 3 modules: Connection, Ping, FSM;
The ping module is encapsulated by the same Ping sample code 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! If you encounter any questions or questions, you can contact me and look forward to communicating with you about iOS development technology (you can ask me directly on my blog or email me).
Actual network connection Status detection under iOS: realreachability