iOS development--reachability and afnetworking determine network connection status

Source: Internet
Author: User

    First, reachabilityListen for notifications of network status changes [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (networkstatechange) Name: Kreachabilitychangednotification object:nil];//Create reachabilityself.conn = [reachability reachabilityforinternetconnection];//starts monitoring the network (notifies Kreachabilitychangednotification once the network status has changed) [Self.conn startnotifier];//Processing Network status change-(void) networkstatechange{//1. Detect WiFi status reachability *wifi = [reachability reachabilityf        Orlocalwifi];        2. Check whether the mobile phone can be on the network (wifi\3g\2.5g) reachability *conn = [reachability reachabilityforinternetconnection]; 3. Determine the network status if ([WiFi currentreachabilitystatus]! = notreachable) {
WiFi NSLog (@ "WiFi"); } else if ([conn currentreachabilitystatus]! = notreachable) {
Do not use WiFi, use the mobile phone to bring the network to the Internet NSLog (@ "Use the mobile phone to bring the network to the Internet"); } else {
No network NSLog (@ "no network"); }}

   Second, afnetworking1. Access to Network monitoring manager Afnetworkreachabilitymanager *manager = [Afnetworkreachabilitymanager sharedmanager];//2. Set the processing after network status change [manager setreachabilitystatuschangeblock:^ (Afnetworkreachabilitystatus status) {
When the network state changes, the block is called
Switch (status) {
Case Afnetworkreachabilitystatusunknown:
Unknown network
NSLog (@ "Unknown network");
Break
Case Afnetworkreachabilitystatusnotreachable:
No network (off-grid)
NSLog (@ "No network (off-Grid)");
Break
Case Afnetworkreachabilitystatusreachableviawwan:
Mobile phone comes with network
NSLog (@ "Mobile phone comes with network");
Break
Case Afnetworkreachabilitystatusreachableviawifi:
Wifi
NSLog (@ "WIFI");
Break
}
}];

3. Start monitoring
[Manager Startmonitoring];

iOS development--reachability and afnetworking determine network connection status

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.