1. Reachability//Notification of monitoring network status changes[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: KreachabilitychangednotificationObject: nil];//Create reachabilitySelf.conn =[reachability reachabilityforinternetconnection];//Start monitoring the network (notify Kreachabilitychangednotification once the network status changes)[Self.conn Startnotifier];//Handling Network status changes- (void) networkstatechange{//1. Detect WiFi statusreachability *wifi =[reachability Reachabilityforlocalwifi]; //2. Check whether the mobile phone can be on the network (wifi\3g\2.5g)reachability *conn =[reachability reachabilityforinternetconnection]; //3. Determine network status if([WiFi currentreachabilitystatus]! = notreachable) {//have wifiNSLog (@"have wifi"); } Else if([conn currentreachabilitystatus]! = notreachable) {//no WiFi, Internet access using your phone's own networkNSLog (@"Use your phone to bring your own network to the Internet"); } Else{//No networkNSLog (@"No network"); }}2. AFN//1. Managers who have access to network monitoringAfnetworkreachabilitymanager *mgr =[Afnetworkreachabilitymanager Sharedmanager];//2. Set up the processing after the network status change[Mgr setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {//when the network state changes, the block is called Switch(status) { CaseAfnetworkreachabilitystatusunknown://Unknown NetworkNSLog (@"Unknown Network"); Break; CaseAfnetworkreachabilitystatusnotreachable://No network (off-grid)NSLog (@"No network (off-grid)"); Break; CaseAfnetworkreachabilitystatusreachableviawwan://mobile phone comes with networkNSLog (@"mobile phone comes with network"); Break; CaseAfnetworkreachabilitystatusreachableviawifi://WIFINSLog (@"WIFI"); Break; }}];//3. Start monitoring[Mgr Startmonitoring];
iOS reachability and AFN determine network connection status