IOS monitors network status changes in real time and ios monitors the status in real time.

Source: Internet
Author: User

IOS monitors network status changes in real time and ios monitors the status in real time.

 

In network applications, you may need to monitor the network status of your devices in real time for two purposes:

(1) let users know their network status and prevent misunderstandings (for example, the application is incompetent)

(2) Smart processing based on the user's network status, saving user traffic and improving user experience

Wi-Fi network: Automatically downloads HD Images

4G/3G network: only downloading thumbnails

No network: Only offline cached data is displayed.

There are two common methods:

(1) Use the Reachablity provided by the apple detection method to detect the network environment of iOS devices

(2) Use AFNetworkReachabilityManager In the AFN framework to listen for changes in the network status

 

1. Apple officially provides a Reachability sample program to help developers detect the network status.

Download the example from the Apple website before using it:

Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

Add Reachability. h and Reachability. m to your project and reference SystemConfiguration. framework.
Three Network statuses are defined in Reachability:

Typedef enum: NSInteger {NotReachable = 0, // ReachableViaWiFi is not connected, // use 3G/GPRS network ReachableViaWWAN // use WiFi network} NetworkStatus;

 

We can start real-time monitoring after the program starts.

// AppDelegate. m @ interface AppDelegate () @ property (nonatomic, strong) Reachability * reachability; @ end // program initiator, start network monitoring-(void) applicationDidFinishLaunching :( UIApplication *) application {// set the NSString * remoteHostName = @ "www.apple.com"; self. reachability = [Reachability reachabilityWithHostName: remoteHostName]; // set the notification function when the network status changes [[nsicationicationcenter defacenter center] addObserver: self selector: @ Selector (reachabilityChanged :) name: @ "kNetworkReachabilityChangedNotification" object: nil]; [self updateStatus];}-(void) reachabilityStatusChange :( NSNotification *) notification {Reachability * curReach = [notification object]; NSParameterAssert ([curReach isKindOfClass: [Reachability class]); [self updateInterfaceWithReachability: curReach];}-(void) updateInterfaceWithReachability :( Reachability *) r Eachability {if (reachability = _ reachability) {NetworkStatus netStatus = [reachability currentReachabilityStatus]; switch (netStatus) {case NotReachable: {NSLog (@ "No network! "); Break;} case ReachableViaWWAN: {NSLog (@" 4G/3G "); break;} case ReachableViaWiFi: {NSLog (@" WiFi "); break ;}}}- (void) dealloc {[_ reachability stopNotifier]; [[nsicationicationcenter defaultCenter] removeObserver: self name: kReachabilityChangedNotification object: nil];}

 

2. Use AFNetworkReachabilityManager In the AFN framework to monitor network status changes

 

// Use the AFN framework to detect network status changes-(void) AFNReachability {// 1. create a network listener manager AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager]; // 2. change the network status of the listener/* AFNetworkReachabilityStatusUnknown = unknown = No network connections = 3G Wi-Fi = WIFI */[manager setReachabilityStatusChangeBlock: ^ (AFNetworkReachabilityStatus status) {switch (status) {case AFNetworkReachabilityStatusUnknown: NSLog (@ "unknown"); break; case AFNetworkReachabilityStatusNotReachable: NSLog (@ "No network"); break; case when: NSLog (@ "3G "); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog (@ "WIFI"); break; default: break ;}}]; // 3. start listening to [manager startMonitoring];}

 

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.