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.

12345678910111213141516171819202122232425262728293031323334353637383940   - (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) {        intnetType = [[networkView valueForKeyPath:@"dataNetworkType"]intValue];        switch(netType) {            case0:                status = SGNetworkStatusNone;                break;            case1://实际上是2G                status = SGNetworkStatusUkonow;                break;            case2:                status = SGNetworkStatus3G;                break;            case3:                status = SGNetworkStatus4G;                break;            case5:                status = SGNetworkStatusWifi;                break;            default:                status = SGNetworkStatusUkonow;                break;        }    }    returnstatus;}

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!

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.