In the network application, sometimes need to monitor the network status of the user device in real time, there are 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 network: Download HD images automatically
4G/3G network: Download thumbnails only
No network: Show only cached data that is offline
Commonly used in the following two ways:
(1), using the Apple view method to detect the iOS device network environment for the library reachablity
(2), use the Afnetworkreachabilitymanager in AFN frame to monitor the change of network state
Apple officially provides a sample program called Reachability, which allows developers to detect network status
Please download the sample from the Apple website before using:
Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
Then add Reachability.h and REACHABILITY.M to your project, and reference Systemconfiguration.framework, you can use it.
3 network states are defined in reachability:
enum : Nsinteger { 0, / / No connection Reachableviawifi, // Use 3g/gprs network Reachableviawwan // use WiFi network } networkstatus;
We can start real-time monitoring after the program starts
//APPDELEGATE.M@interfaceappdelegate () @property (nonatomic, strong) reachability*reachability;@end//program launcher to start network monitoring- (void) Applicationdidfinishlaunching: (UIApplication *) Application {//set up a site for network detectionNSString *remotehostname =@"www.apple.com"; Self.reachability=[reachability reachabilitywithhostname:remotehostname]; //to set the notification function when the network state changes[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name:@"knetworkreachabilitychangednotification" Object: nil]; [Self updatestatus];}- (void) Reachabilitystatuschange: (Nsnotification *) notification{reachability* Curreach = [notificationObject]; Nsparameterassert ([Curreach iskindofclass:[reachabilityclass]]); [Self Updateinterfacewithreachability:curreach];}- (void) Updateinterfacewithreachability: (reachability *) reachability{if(reachability = =_reachability) {NetworkStatus Netstatus=[reachability currentreachabilitystatus]; Switch(netstatus) { Casenotreachable: {NSLog (@"No network! "); Break; } CaseReachableviawwan: {NSLog (@"4g/3g"); Break; } CaseReachableviawifi: {NSLog (@"WiFi"); Break; } } }}- (void) dealloc{[_reachability Stopnotifier]; [[Nsnotificationcenter Defaultcenter] removeobserver:self name:kreachabilitychangednotificationObject: nil];}
Second, use the Afnetworkreachabilitymanager in AFN frame to monitor the change of network state
//Use the AFN framework to detect changes in network status-(void) afnreachability{//1. Create a network listener managerAfnetworkreachabilitymanager *manager =[Afnetworkreachabilitymanager Sharedmanager]; //2. Monitor network status changes /*Afnetworkreachabilitystatusunknown = Unknown afnetworkreachabilitystatusnotreachable = no network Afne Tworkreachabilitystatusreachableviawwan = 3G Afnetworkreachabilitystatusreachableviawifi = WIFI*/[Manager Setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {Switch(status) { CaseAfnetworkreachabilitystatusunknown:nslog (@"Unknown"); Break; CaseAfnetworkreachabilitystatusnotreachable:nslog (@"No network"); Break; CaseAfnetworkreachabilitystatusreachableviawwan:nslog (@"3G"); Break; CaseAfnetworkreachabilitystatusreachableviawifi:nslog (@"WIFI"); Break; default: Break; } }]; //3. Start monitoring[manager startmonitoring];}
iOS monitor network status changes in real time