Network listening to the class for Reachability.h, this Xcode project is not self-contained, need to download from GitHub, when used to remember to import systemconfiguration.framework.
The first is to set up network monitoring in APPDELEGATE.M.
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];
Initialization
_reachability = [reachability reachabilityforinternetconnection]; [_reachability Startnotifier]; [Self updateinterfacewithreachability:_reachability];
Implement two methods:
-(void) reachabilitychanged: (nsnotification*) note;//network connectivity Changes
-(void) Updateinterfacewithreachability: (reachability*) curreach;//processing after connection change
The following is a concrete implementation of the method, which can be changed according to the circumstances:
Connection change-(void) reachabilitychanged: (nsnotification*) Note { reachability* Curreach = [Note Object]; Nsparameterassert ([Curreach iskindofclass: [reachability class]]); [Self Updateinterfacewithreachability:curreach];} After processing the connection changes-(void) Updateinterfacewithreachability: (reachability*) Curreach { ////response to the connection change. networkstatus status = [Curreach currentreachabilitystatus]; if (status = = notreachable) { _isreachable = NO; No connection to the network pops up live uialertview *netalert = [[Uialertview alloc] initwithtitle:@ "cellular data Closed" message:@ " Enable cellular data or wireless LAN to access data "Delegate:nil cancelbuttontitle:@" OK "otherbuttontitles:nil"; [Netalert show]; } else { _isreachable = YES; }}
iOS Development-Network monitoring