Check the actual network connection status in iOS: RealReachability
Preface
Network Connection status detection is a common requirement for iOS app development. For better user experience, we will display local or cached content without a network, and give users appropriate prompts. For most iOS developers, the various Reachablity frameworks changed from Apple's sample code are the general choice to achieve this requirement, such as this library. But in fact, All implementations based on this solution cannot help us detect the real network connection status. They can only detect the local connection status. This situation includes but is not limited to the following scenarios:
1. Currently, public wifi is very popular and requires Web page authentication. No Internet access is available before authentication, but a local connection has been established;
2. There is a local network connection, but the signal is poor and the server cannot be connected;
3. the routing device connected to iOS is not connected to the Internet.
Many cocoachina users have asked and spoke about the problem, for example:
How can I determine whether a device is connected to the Internet? Instead of only Network Connections
[Reachability reachabilityWithHostName:] is useless!
The Reachability example of Apple shows that its capability is limited here:
"Reachability cannot tell your application if you can connect to a participant host, only that an interface is available that might allow a connection, and whether that interface is the WWAN ."
Apple's SCNetworkReachability API tells us more:
"Reachability does not guarantee that the data packet will be actually encoded by the host ."
All the frameworks related to Reachability are implemented at the underlying layer through SCNetworkReachability, so the actual network connection cannot be detected.
In view of this, I hope to build a general, simple, and reliable framework for detecting the actual network connection status, so RealReachability was born.
RealReachability
RealReachability is an open-source repository released to github two months ago. There are more than 1000 stars and more than 200 forks. After several modifications, the current pod version is 1.1.2.
The Project address is as follows:
Https://github.com/dustturtle/realreachability.
This framework was originally developed based on the actual needs of the project. The offline mode has strict requirements on the network connection status, and the actual scenario often encounters "pseudo connection". the Reachability cannot cope with this scenario. The ping capability is introduced after research by multiple parties (this scheme has the smallest traffic overhead and the simplest) to achieve simple actual network connection monitoring. This framework is available after extraction and optimization. We can tell you that this framework has been tested and is still being improved in the appstore application, if you are interested in stability, you can use the latest pod version. (If you have fixed most of the known problems, refer to the usage of the demo ).
The simplest Integration Method for integration is pod: pod 'realachability '. Manual integration: add the RealReachability folder to the project. Dependency: Xcode5.0 +. Support for ARC, iOS6 +. SystemConfiguration. framework needs to be introduced in the project.
The interface design and calling methods are very similar to the Reachability. You can use them seamlessly and conveniently.
Enable network listening (we recommend that you listen in didFinishLaunchingWithOptions ):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [GLobalRealReachability startNotifier]; return YES;}
Listener network change notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kRealReachabilityChangedNotification object:nil];
Sample Code for notification callback:
- (void)networkChanged:(NSNotification *)notification{ RealReachability *reachability = (RealReachability *)notification.object; ReachabilityStatus status = [reachability currentReachabilityStatus]; NSLog(@"currentStatus:%@",@(status));}
Sample Code for triggering real-time network status query:
[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; } }];
Query the current actual network connection status:
ReachabilityStatus status = [reachability currentReachabilityStatus];
Set the host server address for ping detection (optional ):
Note: Make sure that the server supports the ping operation. If this parameter is not set, we use baidu.com as the ping SERVER by default.
You can use your own server or a stable and reliable network address (such as Baidu and qq ). The configuration example is as follows:
GLobalRealReachability.hostForPing = @"www.baidu.com";
Obtain the current data network connection type (Advanced Function ):
WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
This type can be used to optimize the application experience, such as different network types set different network timeout time and so on, on the optimization scheme, you can refer to Ctrip to give the share: http://www.infoq.com/cn/articles/how-ctrip-improves-app-networking-performance
Demo:
We have included a simple Demo project in github's repository and can download and run it directly. You can also refer to the implementation in the demo to call related APIs.
Demo:
Implementation principle of RealReachability architecture diagram:
Vcfrss6/vfjlywxszwfjaguar fiawxpdhm/qtS0tPrC66OpoaM8L3A + DQo8aDIgaWQ9 "conclusion"> conclusion
I hope this framework can help you develop iOS! If you have any questions or usage problems, contact me and look forward to communicating with you about iOS development technology (you can directly ask questions or email me on my blog ).