[IOS Multithreading & Networking-2.8]-Detecting network status

Source: Internet
Author: User

A, DescriptionIn 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 only thumbnails no network: Show only offline cache data 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   Note: This feature can only be tested with a real machine, the iOS simulator, always has WiFi signal, even if you turn off or use airplane mode.   B. Monitoring network StatusReachability use steps to add a frame Systemconfiguration.framework contains the header file #import "Reachability.h"
1- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event {2     //Detecting WiFi status3reachability *wifistatus =[reachability Reachabilityforlocalwifi];4    5     //Detect mobile network status6reachability *netstatus =[reachability reachabilityforinternetconnection];7    8     //Determine network status9     if([Wifistatus currentreachabilitystatus]! =notreachable) {TenNSLog (@"is using WiFi internet access"); One}Else if([Netstatus currentreachabilitystatus]! =notreachable) { ANSLog (@"using your mobile network to surf the internet"); -}Else { -NSLog (@"No network"); the     }   - } -  
C. Real-time monitoring of network statusUse notifications to monitor network status classes to send notifications to the controller when destroying the controller, be sure to delete the notification, stop sending the message
1 //2 //VIEWCONTROLLER.M3 //Reachabilitydemo4 //5 //Created by Hellovoidworld on 15/1/28.6 //Copyright (c) 2015 Hellovoidworld. All rights reserved.7 //8 9 #import "ViewController.h"Ten #import "Reachability.h" One  A @interfaceViewcontroller () -  -@property (nonatomic, strong) reachability *NetworkStatus; the  - @end -  - @implementationViewcontroller +  -- (void) Viewdidload { + [Super Viewdidload]; A     //additional setup after loading the view, typically from a nib. at     -     //real-time monitoring of mobile phone network status -[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (detectnetworkstatus) Name: KreachabilitychangednotificationObject: nil]; -     -     //turn on notification delivery -Self.networkstatus =[reachability reachabilityforinternetconnection]; in [Self.networkstatus Startnotifier]; - } to  +- (void) Dealloc { -     //Stop sending notifications the [Self.networkstatus Stopnotifier]; *     $     //Remember to delete notificationsPanax Notoginseng [[Nsnotificationcenter Defaultcenter] removeobserver:self]; - } the  + //with WiFi A //[WiFi currentreachabilitystatus]! = notreachable the //[conn currentreachabilitystatus]! = notreachable +  - //no wifi, only mobile phone network $ //[WiFi currentreachabilitystatus] = = Notreachable $ //[conn currentreachabilitystatus]! = notreachable -  - //No network the //[WiFi currentreachabilitystatus] = = Notreachable - //[conn Currentreachabilitystatus] = = NotreachableWuyi  the- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event { - [self detectnetworkstatus]; Wu } -  About- (void) Detectnetworkstatus { $     //Detecting WiFi status -reachability *wifistatus =[reachability Reachabilityforlocalwifi]; -     -     //Detect mobile network status Areachability *netstatus =[reachability reachabilityforinternetconnection]; +     the     //Determine network status -     if([Wifistatus currentreachabilitystatus]! =notreachable) { $NSLog (@"is using WiFi internet access"); the}Else if([Netstatus currentreachabilitystatus]! =notreachable) { theNSLog (@"using your mobile network to surf the internet"); the}Else { theNSLog (@"No network"); -     } in } the  the @end About  

[IOS Multithreading & Networking-2.8]-Detecting network status

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.