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 "QYViewController.h"
#import "Reachability.h"
@interface qyviewcontroller ()
@property (nonatomic,strong) reachability *conn;
@end
@implementation Qyviewcontroller
-(void) viewdidload
{
[superviewdidload];
Additional setup after loading the view, typically from a nib.
[[ nsnotificationcenter Defaultcenter] addobserver: self selector: @selector (networkstatechange) name: kreachabilitychangednotification object: nil ];
self. Conn = [reachabilityreachabilityforinternetconnection];
[self. ConnStartnotifier];
}
-(void) Dealloc
{
[self. ConnStopnotifier];
[[nsnotificationcenterdefaultcenter] removeobserver:self];
}
-(void) Networkstatechange
{
[self checknetworkstate];
}
-(void) checknetworkstate
{
//1. detecting wifi status
reachability *wifi = [reachabilityreachabilityforlocalwifi];
//2. detect if the phone can be on the network (wifi\3g\2.5g)
reachability *conn = [reachabilityreachabilityforinternetconnection];
//3. Determine network status
if ([WiFicurrentreachabilitystatus]! = notreachable) {// Have WiFi
NSLog (@ " wifi");
} elseif ([conn currentreachabilitystatus]! = notreachable ) {// do not use WiFi, Use your phone to bring your own network to the Internet
NSLog (@ " use your phone to bring your own network to the internet ");
}Else { // No network
NSLog (@ " no network ");
}
}
iOS Programming-Network monitoring