First, reachabilityListen for notifications of network status changes [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: Kreachabilitychangednotification object:nil];//Create reachabilityself.conn = [reachability reachabilityforinternetconnection];//starts monitoring the network (notifies Kreachabilitychangednotification once the network status has changed) [Self.conn startnotifier];//Processing Network status change-(void) networkstatechange{//1. Detect WiFi status reachability *wifi = [reachability reachabilityf Orlocalwifi]; 2. Check whether the mobile phone can be on the network (wifi\3g\2.5g) reachability *conn = [reachability reachabilityforinternetconnection]; 3. Determine the network status if ([WiFi currentreachabilitystatus]! = notreachable) {
WiFi NSLog (@ "WiFi"); } else if ([conn currentreachabilitystatus]! = notreachable) {
Do not use WiFi, use the mobile phone to bring the network to the Internet NSLog (@ "Use the mobile phone to bring the network to the Internet"); } else {
No network NSLog (@ "no network"); }}
Second, afnetworking1. Access to Network monitoring manager Afnetworkreachabilitymanager *manager = [Afnetworkreachabilitymanager sharedmanager];//2. Set the processing after network status change [manager setreachabilitystatuschangeblock:^ (Afnetworkreachabilitystatus status) {
When the network state changes, the block is called
Switch (status) {
Case Afnetworkreachabilitystatusunknown:
Unknown network
NSLog (@ "Unknown network");
Break
Case Afnetworkreachabilitystatusnotreachable:
No network (off-grid)
NSLog (@ "No network (off-Grid)");
Break
Case Afnetworkreachabilitystatusreachableviawwan:
Mobile phone comes with network
NSLog (@ "Mobile phone comes with network");
Break
Case Afnetworkreachabilitystatusreachableviawifi:
Wifi
NSLog (@ "WIFI");
Break
}
}];
3. Start monitoring
[Manager Startmonitoring];
iOS development--reachability and afnetworking determine network connection status