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 @interfaceYyviewcontroller ()5@property (nonatomic, strong) reachability *Conn;6 @end7 8 @implementationYyviewcontroller9 Ten- (void) Viewdidload One { A [Super Viewdidload]; - -[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: KreachabilitychangednotificationObject: nil]; theSelf.conn =[reachability reachabilityforinternetconnection]; - [Self.conn Startnotifier]; - } - +- (void) Dealloc - { + [Self.conn Stopnotifier]; A [[Nsnotificationcenter Defaultcenter] removeobserver:self]; at } - -- (void) Networkstatechange - { - [self checknetworkstate]; - } in -- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event to { + - } the *- (void) Checknetworkstate $ {Panax Notoginseng //1. Detect WiFi status -reachability *wifi =[reachability Reachabilityforlocalwifi]; the + //2. Check whether the mobile phone can be on the network (wifi\3g\2.5g) Areachability *conn =[reachability reachabilityforinternetconnection]; the + //3. Determine network status - if([WiFi currentreachabilitystatus]! = notreachable) {//have wifi $NSLog (@"have wifi"); $ -}Else if([conn currentreachabilitystatus]! = notreachable) {//no WiFi, Internet access using your phone's own network -NSLog (@"Use your phone to bring your own network to the Internet"); the -}Else{//No networkWuyi theNSLog (@"No network"); - } Wu } - @end About $ //with WiFi - //[WiFi currentreachabilitystatus]! = notreachable - //[conn currentreachabilitystatus]! = notreachable - A //no wifi, only mobile phone network + //[WiFi currentreachabilitystatus] = = Notreachable the //[conn currentreachabilitystatus]! = notreachable - $ //No network the //[WiFi currentreachabilitystatus] = = Notreachable the //[conn Currentreachabilitystatus] = = Notreachable
iOS Development Network-monitoring network status