IOS development-Detects network status (WIFI, 2G/3G/4G)

Source: Internet
Author: User

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] ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.