Get network status for iOS network access

Source: Internet
Author: User

The network condition of the Apple device is changeable, may have the network, may also be disconnected, possibly through the WiFi networking, may also through the cellular data networking, many times we need to know the program currently running in the network condition

We can add Reachability.h and reachability.m to our project in the official Apple Download tool class reachability.

Example 1: View the current network environment (test network status by accessing a page)

reachability *reach = [reachability reachabilitywithhostname:@"http://www.cnblogs.com/zanglitao/"]; Switch([reach Currentreachabilitystatus]) { CaseNotreachable:nslog (@"Cannot access");  Break;  CaseReachableviawifi:nslog (@"Use WiFi");  CaseReachableviawwan:nslog (@"using cellular data"); }

Example 2: Test if your phone is connected to WiFi (or cellular)

if ([[Reachability Reachabilityforlocalwifi] currentreachabilitystatus]! = notreachable) {        NSLog (@ "  WiFi network available ");    }     if ([[Reachability reachabilityforinternetconnection] currentreachabilitystatus]! = notreachable) {        NSLog (  @ " cellular network available ");    }

Example 3: Listening for network changes

We turn on the notification via the Startnotifier method of the Reachability object, and when the network changes reachability sends a KREACHABILITYCHANGEDNOTIFICATION notification to the default notification center

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions{[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (netstatuschange:) Name:kreachabilitychangednotificationObject: nil]; Reachability*reach = [reachability reachabilitywithhostname:@"http://www.cnblogs.com/zanglitao/"]; [Reach Startnotifier];//[reach Stopnotifier];        returnYES;}- (void) Netstatuschange: (Nsnotification *) Note {reachability*reach = [NoteObject]; //get current network statusNetworkStatus *status =[reach Currentreachabilitystatus]; }

Get network status for iOS network access

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.