Detecting Network status
In the network application, the network status of the user device needs to be monitored in real time to
Let users know their network status, to prevent some misunderstandings (such as the application of incompetence)
Intelligent processing based on the user's network status, saving user traffic and improving 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
Reachability Steps to use
Add Frame Systemconfiguration.framework
Add source code
Include header file
#import "Reachability.h"
Extracting tool Classes
常见用法// 是否WIFI+ (BOOL) IsEnableWIFI { return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL) IsEnable3G { return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);}
Network Monitoring
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selectornil];self.netReachability = [Reachability reachabilityForInternetConnection];[self.netReachability startNotifier];- (void)dealloc{ [self.netReachability stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];}
Detecting Network Status instances
Networktool Tool Class
#import <Foundation/Foundation.h>@interface NetworkTool : NSObject/** * 是否WIFI */+ (BOOL)isEnableWIFI;/** * 是否3G */+ (BOOL)isEnable3G;@end
#import "NetWorkTool.h"#import "Reachability.h"@implementation NetworkTool// 是否WIFI+ (BOOL)isEnableWIFI { return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL)isEnable3G { return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);}@end
Implementation class
#import "ViewController.h" #import "Reachability.h" #import "NetworkTool.h" @interface viewcontroller ()@property(nonatomic,Strong) reachability *reachability;@end @implementation viewcontroller - (void) viewdidload{[SuperViewdidload];//Listen for notification of changes in network status[[NsnotificationcenterDefaultcenter] Addobserver: SelfSelector@selector(Networkstatechange) Name:kreachabilitychangednotification object:Nil];//Get Reachability object Self. reachability= [reachability reachabilityforinternetconnection];//Start monitoring network[ Self. reachabilityStartnotifier];} - (void) dealloc{[ Self. reachabilityStopnotifier]; [[NsnotificationcenterDefaultcenter] Removeobserver: Self];} - (void) networkstatechange{NSLog(@"The network state has changed"); [ SelfChecknetworkstate];} - (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[ SelfChecknetworkstate];}
#pragma mark-app Application network type changes will be called- (void) networkstatechange{if([Networktool Isenablewifi]) {Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Friendly and helpful"message:@"You now have a network environment of wifi! Start enjoying it! "Delegate Selfcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil]; [Alert show]; }Else if([Networktool isenable3g]) {Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Friendly and helpful"message:@"You now network environment for 3G/4G Network!" "Delegate Selfcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil]; [Alert show]; }Else{Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Friendly and helpful"message:@"You don't currently have a network to connect to or have dropped!" "Delegate Selfcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil]; [Alert show]; }}
iOS Dev-Detect network status (WiFi, 2g/3g/4g)