About network status monitoring real-time, creating a single Network Monitor (not real-time)

Source: Internet
Author: User

#pragma mark Network condition monitoring

-(void) checknetworkstatus{

Create a URL for testing

Nsurl *url=[nsurl urlwithstring:@ "http://www.baidu.com"];

Afhttprequestoperationmanager *operationmanager=[[afhttprequestoperationmanager Alloc]initWithBaseURL:url];

According to different network status changes to do the corresponding processing

[Operationmanager.reachabilitymanager setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {

Switch (status) {

Case Afnetworkreachabilitystatusreachableviawwan:

[Self alert:@ "2g/3g/4g Connection."];

Break

Case Afnetworkreachabilitystatusreachableviawifi:

[Self alert:@ "WiFi Connection."];

Break

Case Afnetworkreachabilitystatusnotreachable:

[Self alert:@ "Network not found."];

Break

Default

[Self alert:@ "Unknown."];

Break

}

}];

Start monitoring

[Operationmanager.reachabilitymanager startmonitoring];

}

-(void) checknetworkstatus{

Create a URL for testing

Nsurl *url=[nsurl urlwithstring:@ "http://www.apple.com"];

Afhttpsessionmanager * Operationmanager=[[afhttpsessionmanager Alloc]initwithbaseurl:url];

According to different network status changes to do the corresponding processing

[Operationmanager.reachabilitymanager setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {

Switch (status) {

Case Afnetworkreachabilitystatusreachableviawwan:

[Self alert:@ "2g/3g/4g Connection."];

[Svprogresshud showsuccesswithstatus:@ "2g/3g/4g Connection."];

Dnlog (@ "2g/3g/4g Connection.");

Break

Case Afnetworkreachabilitystatusreachableviawifi:

[Svprogresshud showsuccesswithstatus:@ "WiFi Connection."];

Dnlog (@ "WiFi Connection.");

Break

Case Afnetworkreachabilitystatusnotreachable:

[Svprogresshud showerrorwithstatus:@ "Network not found."];

Dnlog (@ "Network not found.");

Break

Default

[Svprogresshud showerrorwithstatus:@ "Unknown."];

Dnlog (@ "Unknown.");

Break

}

}];

Start monitoring

[Operationmanager.reachabilitymanager startmonitoring];

}

/**************** detects network status and returns no network-like too ****************/

To import header files

+ (BOOL) Networkstatefalse {

reachability* reach = [reachability reachabilitywithhostname:@ "www.baidu.com"];

[Reach Startnotifier];

return ([[reachability reachabilityforinternetconnection] currentreachabilitystatus] = = notreachable);

}

About network status monitoring real-time, creating a single Network Monitor (not real-time)

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.