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