Use reachability to detect network status in iOS development

Source: Internet
Author: User

I. Confirm the network environment 3G/WIFI

1. When you add source files and framework to develop Web applications, you must confirm the network environment, connection conditions, and other information. If they are not processed, they will not be reviewed by Apple. Apple's routine Reachability describes how to obtain/detect the network status. To use Reachability in an application, complete the following two steps: 1.1. add source file: to use Reachability in your program, you only need to Reachability in this routine. h and Reachability. copy m to your project. The h and m files for Reachability can be downloaded here: https://github.com/tonymillion/Reachability/blob/master/Reachability.h (supports ARC and GCD) 1.2. Add framework: Add SystemConfiguration. framework to the project. Example: 2. network Status Reachability. h defines three network statuses: typedef enum {NotReachable = 0, // ReachableViaWiFi is not connected, // use 3G/GPRS network ReachableViaWWAN // use WiFi network} NetworkStatus; therefore, you can check the network status as follows: Reachability * r = [Reachability reachabilityWithHostName: @ "www.apple.com"]; switch ([r currentReachabilityStatus]) {case NotReachable: // No network connection break; case ReachableViaWWAN: // use a 3G network break; case ReachableViaWiFi: // use a Wi-Fi network break;} 3. when the current network Environment Program is started, if you want to check the available network environment, you can // check whether wifi + (BOOL) IsEnableWIFI {return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus]! = NotReachable);} // whether it is 3G + (BOOL) IsEnable3G {return ([Reachability reachabilityForInternetConnection] currentReachabilityStatus]! = NotReachable);} example:-(void) viewWillAppear :( BOOL) animated {if ([Reachability reachabilityForInternetConnection]. currentReachabilityStatus = NotReachable) & ([Reachability reachabilityForLocalWiFi]. currentReachabilityStatus = NotReachable) {self. navigationItem. hidesBackButton = YES; [self. navigationItem setLeftBarButtonItem: nil animated: NO] ;}} 4. real-time notification of the network connection status is checked in real time, and notification is also necessary in network applications. When the connection status changes, you must promptly notify the user of Reachability 1.5. // My. appDelegate. h # import "Reachability. h "@ interface MyAppDelegate: NSObject <UIApplicationDelegate> {NetworkStatus remoteHostStatus;} @ property NetworkStatus remoteHostStatus; @ end // My. appDelegate. m # import "MyAppDelegate. h "@ implementation MyAppDelegate @ synthesize remoteHostStatus; // update the network status-(void) updateStatus {self. remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];} // notification network status-(void) reachabilityChanged :( NSNotification *) note {[self updateStatus]; if (self. remoteHostStatus = NotReachable) {alert * alert = [[delealloc] initWithTitle: NSLocalizedString (@ "AppName", nil) message: NSLocalizedString (@ "NotReachable", nil) delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert show]; [alert release] ;}}// program initiator, enable Network Monitoring-(void) applicationDidFinishLaunching :( UIApplication *) application {// sets the network detection site [[Reachability sharedReachability] setHostName: @ "www.apple.com"]; [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled: YES]; // set the notification function when the network status changes [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (reachabilityChanged :) name: @ "kNetworkReachabilityChangedNotification" object: nil]; [self updateStatus];}-(void) dealloc {// Delete the notification object [[nsicationicationcenter defacenter center] removeObserver: self]; [window release]; [super dealloc];} reachability 2.0 // MyAppDelegate. h @ class Reachability; @ interface MyAppDelegate: NSObject <UIApplicationDelegate> {Reachability * hostReach;} @ end // MyAppDelegate. m-(void) reachabilityChanged :( NSNotification *) note {Reachability * curReach = [note object]; NSParameterAssert ([curReach isKindOfClass: [Reachability class]); networkStatus status = [curReach failed]; if (status = NotReachable) {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "AppName" "message: @" NotReachable "delegate: nil cancelButtonTitle: @ "YES" otherButtonTitles: nil]; [alert show]; [alert release] ;}}-(void) applicationDidFinishLaunching :( UIApplication *) application {//... // monitor network conditions [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (reachabilityChanged :) name: kReachabilityChangedNotification object: nil]; hostReach = [[Reachability reachabilityWithHostName: @ "www.google.com"] retain]; hostReach startcycler]; //...}

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.