Several methods of determining network connection status by IOS

Source: Internet
Author: User

(i). Reachability

Add source files and framework

Apple's official example, reachability , describes how to obtain and detect the current network status of a device. In your program, you need to copy the project's Reachability.h and REACHABILITY.M to your project, and you need to add systemconfiguration.framework to the project,

Such as:

Notification of monitoring network status changes

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: Kreachabilitychangednotification Object: nil];

Create reachability

Self.conn = [reachability reachabilityforinternetconnection];

Start monitoring the network (notify Kreachabilitychangednotification once the network status changes)

[Self.conn Startnotifier];

Handling Network status changes

-(void) Networkstatechange

{

//1. Detect WiFi status

reachability *wifi = [reachability Reachabilityforlocalwifi];

//2. Detect if the phone can be on the network (wifi\3g\2.5g)

reachability *conn = [reachability reachabilityforinternetconnection];

//3. Determine network status

if ([WiFi currentreachabilitystatus]! = notreachable) { //WiFi available

NSLog (@ "WiFi");

} else if ([conn currentreachabilitystatus]! = notreachable) { //not using WiFi, use your phone to bring your own network to the Internet

NSLog (@ "Use your phone to bring your own network to the internet");

} else { //no network

NSLog (@ "no network");

}

}

(b). AFN

1. Managers who have access to network monitoring

Afnetworkreachabilitymanager *mgr = [Afnetworkreachabilitymanager Sharedmanager];

2. Set up the processing after the network status change

[Mgr setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {

//When the network status changes, the block is called

switch (status) {

Case Afnetworkreachabilitystatusunknown: //Unknown network

NSLog (@ "Unknown network");

break;

Case afnetworkreachabilitystatusnotreachable: //No network (off-grid)

NSLog (@ "No network (off-grid)");

break;

Case Afnetworkreachabilitystatusreachableviawwan: //Phone comes with network

NSLog (@ "mobile phone comes with network");

break;

Case Afnetworkreachabilitystatusreachableviawifi: //WIFI

NSLog (@ "WIFI");

break;

}

}];

3. Start monitoring

[Mgr Startmonitoring];

(iii) Get the network type from the status bar, with the following code:

-(NSString *) getnetworkstates{

UIApplication *app = [UIApplication sharedapplication];

Nsarray *children = [[App valueforkeypath:@ "StatusBar"]valueforkeypath:@ "Foregroundview"]subviews];

NSString *state = [[NSString alloc]init];

int netType = 0;

Get to the network return code

For (ID child in children) {

if ([Child iskindofclass:nsclassfromstring (@ "Uistatusbardatanetworkitemview")]) {

Get to status bar

NetType = [[Child valueforkeypath:@ "Datanetworktype"]intvalue];

Switch (netType) {

Case 0:

state = @ "No network";

No network mode

Break

Case 1:

State = @ "2G";

Break

Case 2:

State = @ "3G";

Break

Case 3:

State = @ "4G";

Break

Case 5:

{

state = @ "WiFi";

Break

Default

Break

}

}

}

Select by Status

}

return state;

}

The rationale is to get internal property StatusBar from the uiapplication type through Valueforkey. Then filter an internal type

(Uistatusbardatanetworkitemview), finally return his Datanetworktype property, get the network according to the status bar

Status, can distinguish between 2G, 3G, 4G, WIFI, System method, relatively fast, bad is in case the connection WiFi is not connected,

Not recognized.

Several methods of determining network connection status by IOS

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.