reachability Detecting Network status

Source: Internet
Author: User

In the current project development, it is necessary to monitor whether the network is normal. reachability detection network can detect WiFi 3G wireless LAN

Using reachability

Download the reachability, add the reachability to the project, and add it to the class that will detect the network.

#import <Reachability.h>

Turn on network status monitoring

[[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];

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

[Self.reachability startnotifier];//starts listening

In method reachabilitychanged: write about changing the network to do

Call method when network state changes

-(void) reachabilitychanged: (nsnotification *) Note

{

reachability *currreach = [Note Object];

Nsparameterassert ([Currreach iskindofclass:[reachability class]]);

Self.status = [Currreach currentreachabilitystatus];

self.isreachable = YES;

Switch (self.status) {

Case Notreachable:

{

Uialertview *alertview = [[Uialertview alloc]initwithtitle:@ ' network status ' message:@ ' Pro! No network! "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];

[Alertview show];

self.isreachable = NO;

}

Break

Case Reachableviawifi:

{

Uialertview *alertview = [[Uialertview alloc]initwithtitle:@ ' network status ' message:@ ' connected wifi! "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];

[Alertview show];

self.isreachable = YES;

}

Break

Case Reachableviawwan:

{

self.isreachable = YES;

}

Break

Default

Break

}

}

Can also be written directly in the appdelegate.

reachability Detecting Network status

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.