I. Description
In the network application, the network status of the user device needs to be monitored in real time for two purposes:
(1) Let users know their network status, to prevent some misunderstandings (such as the application of incompetence)
(2) According to the user's network status intelligent processing, save user traffic, improve the user experience
WIFI\3G network: Automatically download HD images
Low-speed network: Download thumbnails only
No network: Show only cached data that is offline
Apple officially provides a sample program called Reachability, which allows developers to detect network status
Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
Second, monitoring network status
Reachability Steps to use
Add Frame Systemconfiguration.framework
Add source code
Include header file
#import "Reachability.h"
code example:
1#import"YYViewController.h"2#import"Reachability.h"34@interfaceYyviewcontroller ()5 @property (nonatomic, strong) reachability *Conn6@end78@implementationYyviewcontroller910-(void) Viewdidload11{12[Super Viewdidload];13[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: KreachabilitychangednotificationObject: nil];Self.conn =[Reachability reachabilityforinternetconnection];16[Self.conn Startnotifier];17}1819-(void) Dealloc20{21st[Self.conn Stopnotifier];22[[Nsnotificationcenter Defaultcenter] removeobserver:self];23}2425-(void) Networkstatechange26{27[Self checknetworkstate];28}2930-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event31{3233}3435-(void) checknetworkstate36{37//1. Detect WiFi Statusreachability *wifi =[Reachability Reachabilityforlocalwifi];3940//2. Check whether the mobile phone can be on the network (wifi\3g\2.5g)reachability *conn =[Reachability reachabilityforinternetconnection];4243//3. Determine network status44if ([WiFi currentreachabilitystatus]! = notreachable) {//Have WiFiNSLog (@"Have WiFi");4647}Elseif ([conn currentreachabilitystatus]! = notreachable) {//No WiFi, Internet access using your phone's own networkNSLog (@"Use your phone to bring your own network to the Internet");4950}else {//No network51NSLog (@"No network");53}54}55@end5657//With WiFi58//[WiFi currentreachabilitystatus]! = notreachable59//[Conn Currentreachabilitystatus]! = notreachable60 61 // No wifi, only use the mobile network 62 //63 //< Span style= "color: #008000;" > [conn Currentreachabilitystatus]! = Notreachable64 65 // no network 66 // [WiFi currentreachabilitystatus] = = Notreachable67 // [conn Currentreachabilitystatus] = = notreachable
Monitoring network status of iOS development