Check the actual network connection status in iOS, and check the network connection status in ios.

Source: Internet
Author: User
Tags fsm

Check the actual network connection status in iOS, and check the network connection status in ios.

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 written by the host ."

The framework related to Reachability implements network detection through SCNetworkReachability at the underlying layer, so it cannot detect the actual network connection.

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 one month ago. The project address is as follows: https://github.com/dustturtle/realreachability. In just one month, I have gained more than 100 stars, mainly from foreign friends. Recently, I also listed the OC trend in github, which really surprised me. 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 in the appstore application and has been tested for a long time, so you can use it with confidence.

Integration and usage

  • Integration

The simplest integration method is pod: pod 'realachability '.

Manual integration: add the RealReachability folder to the project.

Dependency: Xcode5.0 +, supports ARC, iOS6 +. SystemConfiguration. framework.

  • Introduction

The interface design and calling methods are very similar to the Reachability. You can use them seamlessly and conveniently. Enable network listening:

12345    [GLobalRealReachability startNotifier];    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(networkChanged:)                                                 name:kRealReachabilityChangedNotification                                               object:nil];

Callback code example:

123456 - (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:

123456789101112131415161718192021222324 [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:

1 ReachabilityStatus status = [reachability currentReachabilityStatus];

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

RealReachability architecture:

RealReachability Architecture Overview diagram RealReachability mainly consists of three modules: connection, ping, and FSM. The Ping module encapsulates the ping sample code that is also provided by Apple, the connection module implements local state detection based on SCNetworkReachability. The FSM module is a finite state machine. The connection module and Ping module work collaboratively through FSM status management control, and the configurable timing policies and other business logic optimization finally achieve our implementation.

PS: the connection module and ping module can also be used independently to provide local network detection and ping capabilities. Interested readers can also try it (for details about the call method, refer to RealReachability open source code ).

Conclusion

I hope this framework can help you develop iOS! Contact me if you have any questions or usage problems.

I will also continue to maintain and optimize this framework. I also hope that interested friends can go to pull request on github! Open source is more exciting for you! Https://github.com/dustturtle/RealReachability think useful just a star Oh!

Q: one-click call to programmer Q & A artifacts, one-to-one service, developer programming required official website: www.wenaaa.com

QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.