iOS real-time monitoring of network status changes

Source: Internet
Author: User

In the network application, sometimes need to monitor the network status of the user device in real time, there are 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 network: Download HD images automatically

4G/3G network: Download thumbnails only

No network: Show only cached data that is offline

Commonly used in the following two ways:

(1), using the Apple view method to detect the iOS device network environment for the library reachablity

(2), use the Afnetworkreachabilitymanager in AFN frame to monitor the change of network state

Apple officially provides a sample program called Reachability, which allows developers to detect network status

Please download the sample from the Apple website before using:

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

Then add Reachability.h and REACHABILITY.M to your project, and reference Systemconfiguration.framework, you can use it.
3 network states are defined in reachability:

enum : Nsinteger {    0,  / / No connection    Reachableviawifi,  //  Use 3g/gprs network    Reachableviawwan  // use WiFi network } networkstatus;

We can start real-time monitoring after the program starts

//APPDELEGATE.M@interfaceappdelegate () @property (nonatomic, strong) reachability*reachability;@end//program launcher to start network monitoring- (void) Applicationdidfinishlaunching: (UIApplication *) Application {//set up a site for network detectionNSString *remotehostname =@"www.apple.com"; Self.reachability=[reachability reachabilitywithhostname:remotehostname]; //to set the notification function when the network state changes[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name:@"knetworkreachabilitychangednotification" Object: nil]; [Self updatestatus];}- (void) Reachabilitystatuschange: (Nsnotification *) notification{reachability* Curreach = [notificationObject]; Nsparameterassert ([Curreach iskindofclass:[reachabilityclass]]); [Self Updateinterfacewithreachability:curreach];}- (void) Updateinterfacewithreachability: (reachability *) reachability{if(reachability = =_reachability) {NetworkStatus Netstatus=[reachability currentreachabilitystatus]; Switch(netstatus) { Casenotreachable: {NSLog (@"No network! ");  Break; }             CaseReachableviawwan: {NSLog (@"4g/3g");  Break; }             CaseReachableviawifi: {NSLog (@"WiFi");  Break; }        }    }}- (void) dealloc{[_reachability Stopnotifier]; [[Nsnotificationcenter Defaultcenter] removeobserver:self name:kreachabilitychangednotificationObject: nil];}

Second, use the Afnetworkreachabilitymanager in AFN frame to monitor the change of network state

//Use the AFN framework to detect changes in network status-(void) afnreachability{//1. Create a network listener managerAfnetworkreachabilitymanager *manager =[Afnetworkreachabilitymanager Sharedmanager]; //2. Monitor network status changes    /*Afnetworkreachabilitystatusunknown = Unknown afnetworkreachabilitystatusnotreachable = no network Afne Tworkreachabilitystatusreachableviawwan = 3G Afnetworkreachabilitystatusreachableviawifi = WIFI*/[Manager Setreachabilitystatuschangeblock:^(afnetworkreachabilitystatus status) {Switch(status) { CaseAfnetworkreachabilitystatusunknown:nslog (@"Unknown");  Break;  CaseAfnetworkreachabilitystatusnotreachable:nslog (@"No network");  Break;  CaseAfnetworkreachabilitystatusreachableviawwan:nslog (@"3G");  Break;  CaseAfnetworkreachabilitystatusreachableviawifi:nslog (@"WIFI");  Break; default:                 Break;    }    }]; //3. Start monitoring[manager startmonitoring];}

iOS monitor network status changes in real time

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.