iOS Development Network-monitoring network status
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"
#import "YYViewController.h" #import "Reachability.h" @interface Yyviewcontroller () @property (nonatomic, Strong) reachability *conn; @end @implementation yyviewcontroller-(void) viewdidload{[Super Viewdidload]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: Kreachabilitychangednotification Object:nil]; Self.conn = [reachability reachabilityforinternetconnection]; [Self.conn startnotifier];} -(void) dealloc{[Self.conn Stopnotifier]; [[Nsnotificationcenter Defaultcenter] removeobserver:self];} -(void) networkstatechange{[self checknetworkstate];} -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event{}-(void) checknetworkstate{//1. Detect WiFi status Reachabil ity *wifi = [reachability Reachabilityforlocalwifi]; 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) {//have 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 phone to bring the network to the Internet"); } else {//no network NSLog (@ "no network"); }} @end//with wifi//[WiFi currentreachabilitystatus]! = notreachable//[conn currentreachabilitystatus]! = notreachable// No WiFi, only mobile network//[WiFi currentreachabilitystatus] = = notreachable//[Conn currentreachabilitystatus]! = notreachable/ /no network//[WiFi currentreachabilitystatus] = = notreachable//[Conn Currentreachabilitystatus] = = notreachable
iOS Basics-Network-Monitor network status