iOS Perfect Network status-judging tool

Source: Internet
Author: User

Most apps are heavily dependent on the Web, and a user-friendly app must take into account the changing state of the network. Iossinger generally uses the Reachability class to detect changes in the network.

Reachability

This is a class in the Apple development document, officially written, to determine the network changes, including no network, WiFi, and three cases of cellular. The Reachability class is actually Apple's encapsulation of the Scnetworkreachability API, which is simpler to use and is no longer described here. To say its advantages and disadvantages:

Advantages

Simple to use, only one class, official and demo, easy to get started

High sensitivity, basic network changes, the basic can be judged immediately

Ability to determine the switching of a network state, such as a 2g/3g/4g switch

Disadvantages

Cannot tell if the router itself can be networked

Can connect to a specified server, such as a server outside the domestic access wall

There is a net, but the signal is very poor, the Internet is very slow, with no net. At this time should be considered no net.

Solution Solutions

In fact, reachability is very good, but in fact, the client arrives at the server need a lot of "level", such as routers, telecommunications servers, firewalls and so on. In fact, the answer is to solve a problem: whether the client can successfully access the server. Here is another official class: Simpleping point I see

Simpleping

Simpleping is also a class of official documents designed to ping the server to determine whether the client can connect to the specified server. Ping is similar to the heartbeat packet function, ping the server for a period of time to see if it is unimpeded. So ping is not able to determine the network changes in time, There will be a certain delay. Maybe everyone has guessed my train of thought, yes, put them together. Here's what I'm thinking about:

First use reachability to determine whether the device is networked, and if it can connect to the server ping to check

If Reachability is judged to have a network, and Ping is also judged to have a network, then it is true that there is a net, otherwise there is no net.

Although Ping can determine whether the client to the server is unblocked, but due to network jitter or network is weak, and so on, there may be a ping failure, the solution is to add the number of failures limit, over the limit is considered off the network.

2/3/4G switch, reachability Although the network changes detected, but the type or cellular movement, can not give a specific network type. This can be judged by getting the properties on the status bar.

  -  (Sgnetworkstatus) networkdetailstatus{    uiapplication *app =  [uiapplication sharedapplication];    uiview *statusbar = [app  valueforkeypath:@ "StatusBar"];    uiview *foregroundview = [statusbar  valueforkeypath:@ "Foregroundview"];        uiview * networkview = nil;        for  (UIView *childView  in foregroundview.subviews)  {        if  ([ Childview iskindofclass:nsclassfromstring (@ "Uistatusbardatanetworkitemview")])  {             networkView = childView;         }    }         Sgnetworkstatus status = sgnetworkstatusnone;        if  (NetworkView)  {         int nettype = [[networkview valueforkeypath:@ " Datanetworktype "]intvalue];        switch  (netType)  {             case 0:                 status = SGNetworkStatusNone;                 break;             case 1://is actually 2g    .             status =  sgnetworkstatusukonow;                 break;            case 2:                 status = SGNetworkStatus3G;                 break;             case 3:                 status = SGNetworkStatus4G;                 break;             case 5:                 status = SGNetworkStatusWifi;                 break;             defauLt:                status  = SGNetworkStatusUkonow;                 break;        }    }     return status;}

The good news is that even if the status bar is hidden, judgment still works!

Other details

By default, host for ' www.baidu.com ', do not spray me, not to Baidu advertising, but because Baidu is really only suitable to judge whether there is a net, because the response is really fast. Of course, you can also use your own server address, so more real, In case your home server is very slag or suddenly crash can also be a perfect judge.

When judging a specific network type, you actually get the private property of the control using KVC, and there is no case of rejection because of the online response, so don't worry. If this is the reason, please contact me for the first time to change.

How global notifications and proxies are supported. The default global send notification, if the ' delegate ' attribute is set, then only the agent will be notified, not the global notification. If you want to coexist in two ways, you can create a new ' Sgnetobserver ' object.

PS: It looks like Apple's official words are ' the private API that called the system will be rejected '.

Support Simulator, support Ipv4,ipv6

Detailed code here: GitHub address support cocoapods, welcome to use!

Finally, if there is anything wrong, please leave a message.

iOS Perfect Network status-judging tool

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.