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"
code example:
1 #import "YYViewController.h" 2 #import "Reachability.h" 3 4 @interface Yyviewcontroller () 5 @property (nonatomic, str ONG) reachability *conn; 6 @end 7 8 @implementation Yyviewcontroller 9-(void) viewDidLoad11 {viewdidload];13 [Nsnoti Ficationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: Kreachabilitychangednotification object:nil];15 self.conn = [reachability reachabilityforinternetconnection];16 [s Elf.conn startnotifier];17}18-(void) dealloc20 {self.conn stopnotifier];22 [[Nsnotificationcenter Default Center] removeobserver:self];23}24-(void) NetworkStateChange26 {[Self checknetworkstate];28}29-(void) TOU Chesbegan: (Nsset *) touches withevent: (uievent *) event31 {38}34-(void) CheckNetworkState36 {37//1. Detect WiFi status reachability *wifi = [reachability reachabilityforlocalwifi];39 40//2. Detect if the phone can be on the network (wifi\3g\2.5g) Reachab ility *conn = [reachability reACHABILITYFORINTERNETCONNECTION];42 43//3. Determine the network status of the IF ([WiFi currentreachabilitystatus]! = notreachable) {/ /have wifi45 NSLog (@ "WiFi"); ([conn currentreachabilitystatus]! = notreachable) {//Not used WiFi, using the mobile phone to bring the network to the Internet NSLog (@ "Use the mobile phone to bring the internet"); [] else {//no network NSLog (@ "No network") ;}54}55 @end56 57//with WIFI58//[WiFi currentreachabilitystatus]! = NotReachable59//[conn Currentreachabilitysta TUS]! = NotReachable60 61//No WiFi, only mobile phone network (WiFi currentreachabilitystatus) = = NotReachable63//[conn CURRENTREAC Habilitystatus]! = NotReachable64 65//No network//[WiFi currentreachabilitystatus] = = NotReachable67//[conn Currentreach Abilitystatus] = = notreachable
iOS Development Network chapter-monitoring network status