IOS detects the network connection status)

Source: Internet
Author: User

Download the example from the apple website before use: Click here to download

Add reachability. h and reachability. m to your project and reference systemconfiguration. framework.

Three Network statuses are defined in reachability:

// The network state of the device for reachability 1.5.typedef Enum {notreachable = 0, // No reachableviacarrierdatanetwork is connected, // use 3g/GPRS network reachableviawifinetwork // use WiFi network} networkstatus; // The network state of the device for reachability 2.0.typedef Enum {notreachable = 0, // No connection reachableviawifi, // use 3g/GPRS network reachableviawwan // use WiFi network} networkstatus;

 

For example, to check the connection status of a specific site, you can use the following code:

Reachability * r = [reachability reachabilitywithhostname: @ "www.apple.com"]; Switch ([R currentreachabilitystatus]) {Case notreachable: // No network connection break; Case reachableviawwan: // use 3G network break; Case reachableviawifi: // use WiFi network break ;}

 

Check the current network environment:

// Whether WiFi + (bool) isenablewifi {return ([reachability reachabilityforlocalwifi] currentreachabilitystatus]! = Notreachable);} // whether it is 3G + (bool) isenable3g {return ([reachability reachabilityforinternetconnection] currentreachabilitystatus]! = Notreachable );}

 

Real-time notification of connection status

Real-time check of network connection status and notification are also necessary in network applications. When the connection status changes, you must promptly notify the user. The reachability1.5 and 2.0 versions have some changes.

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]; //...}

 

Self conversion: Yi Feiyang's blog

 

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.