IOS development-Detects network status (WIFI, 2G/3G/4G)
Detect Network Status
In network applications, You need to monitor the network status of your devices in real time.
Let users know their network status and prevent misunderstandings (such as the inability to blame applications)
Smart processing based on the user's network status, saving user traffic and improving user experience
WIFI \ 3G network: Automatically downloads HD Images
Low-speed network: only downloading thumbnails
No network: Only offline cached data is displayed.
Apple officially provides an example program called Reachability to help developers detect the network status.
Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
Reachability usage steps
Add framework SystemConfiguration. framework
Add source code
Include header files
#import "Reachability.h"
Extraction Tool
Common usage // whether WIFI + (BOOL) IsEnableWIFI {return ([Reachability reachabilityForLocalWiFi] currentReachabilityStatus]! = NotReachable);} // whether it is 3G + (BOOL) IsEnable3G {return ([Reachability reachabilityForInternetConnectionen] currentReachabilityStatus]! = NotReachable );}
Network Monitoring
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];self.netReachability = [Reachability reachabilityForInternetConnection];[self.netReachability startNotifier];- (void)dealloc{ [self.netReachability stopNotifier]; [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];}
Detect Network Status instances
NetWorkTool
# Import
@ Interface NetworkTool: NSObject/*** whether WIFI */+ (BOOL) isEnableWIFI;/*** whether 3G */+ (BOOL) isEnable3G; @ end
# Import "NetWorkTool. h" # import "Reachability. h" @ implementation NetworkTool // whether WIFI + (BOOL) isEnableWIFI {return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus]! = NotReachable);} // whether it is 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 {[super viewDidLoad]; // notification of network status change [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (networkStateChange) name: kReachabilityChangedNotification object: nil]; // obtain the Reachability object self. reachability = [Reachability reachabilityForInternetConnection]; // starts monitoring the network [self. reachability startNotifier];}-(void) dealloc {[self. reachability stopNotifier]; [[nsicationcenter center defaultCenter] removeObserver: self];}-(void) networkStateChange {NSLog (@ "network status changed"); [self checkNetworkState];} -(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {[self checkNetworkState];}
# If the network type of The pragma mark-App application changes, the following call will be performed:-(void) networkStateChange {if ([NetWorkTool isEnableWIFI]) {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "tip" message: @ "your current network environment is wifi! Get started! "Delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alert show];} else if ([NetWorkTool isEnable3G]) {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "tip" message: @ "your current network environment is 3G/4G! "Delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alert show];} else {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "tip" message: @ "You have no connected network or are offline! "Delegate: self cancelButtonTitle: @" OK "otherButtonTitles: nil, nil]; [alert show] ;}}