When doing iOS development, we need to monitor/listen to the network condition, Apple provides Reachability.h, reachability.m.
Import Reachability.h
We can write it inside the Viewdidload method of Mainviewcontroller:
[Self checkreachability];
After that, the method is as follows
#pragma mark#pragma Mark reachability methods#pragma mark-(void) checkreachability{[[Nsnotificationcenter defaultCent ER] addobserver:self selector: @selector (reachabilitychanged:) name:kreachabilitychangednotification Object:nil]; self.reachability = [reachability reachabilityforinternetconnection]; [Self.reachability Startnotifier]; [Self updateInterfaceWithReachability:self.reachability];} /*! * Called by reachability whenever status changes. */-(void) reachabilitychanged: (nsnotification *) note{reachability* Curreach = [Note Object]; Nsparameterassert ([Curreach iskindofclass:[reachability class]]); [Self Updateinterfacewithreachability:curreach];} -(void) Updateinterfacewithreachability: (reachability *) reachability{networkstatus status = [reachability Currentreachabilitystatus]; Appdelegate *appdelegate = ((Appdelegate *) [[uiapplication Sharedapplication] delegate]); if (status = = notreachable) {//no Internet NSLog (@ "No Internet"); appdelegate.isnetworkreachable = NO; [_reachabilityimage setimage:[uiimage imagenamed:@ "Stop-32.png"]; } else if (status = = Reachableviawifi) {//wifi NSLog (@ "Reachable WiFi"); appdelegate.isnetworkreachable = YES; [_reachabilityimage setimage:[uiimage imagenamed:@ "Airport.png"]; } else if (status = = Reachableviawwan) {//3g NSLog (@ "Reachable 3G"); appdelegate.isnetworkreachable = YES; [_reachabilityimage setimage:[uiimage imagenamed:@ "Wwan5.png"]; }}
iOS Development Monitor network connection, change, disconnect