Reachability is used to determine whether a network is available anywhere

Source: Internet
Author: User
#import <UIKit/UIKit.h>#import "Reachability.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) Reachability *reach;@end


# Import "appdelegate. H "# import" mainviewcontroller. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds]; // override point for customization after application launch. self. window. backgroundcolor = [uicolor whitecolor]; self. window. rootvi Ewcontroller = [[mainviewcontroller alloc] init]; // enable the network condition listening [[nsicationicationcenter defacenter center] addobserver: Self selector: @ selector (reachabilitychanged :) name: kreachabilitychangednotification object: nil]; self. reach = [reachability reachabilitywithhostname: @ "www.baidu.com"]; [self. reach startnotifier]; // start listening. A run loop [self. window makekeyandvisible]; return yes;} // notification-(void) reachabilitycha Nged :( nsnotification *) Note {reachability * Reach = [Note object]; nsparameterassert ([reach iskindofclass: [reachability class]); networkstatus status = [reach currentreachabilitystatus]; //// check whether it is WiFi // nslog (@ "% d", ([reachability reachabilityforlocalwifi] currentreachabilitystatus]! = Notreachable); // used to check whether it is 3 GB // nslog (@ "% d", ([reachability reachabilityforinternetconnection] currentreachabilitystatus]! = Notreachable); If (status = notreachable) {uialertview * alertview = [[uialertview alloc] initwithtitle: Nil message: @ "Network disconnected" delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alertview show]; nslog (@ "Notification says unreachable");} else if (status = reachableviawwan) {uialertview * alertview = [[uialertview alloc] initwithtitle: Nil message: @ "mobile network" delegate: Nil cancelbuttontitle: @ "OK" failed: Nil]; [alertview show]; nslog (@ "Notification says mobilenet");} else if (status = reachableviawifi) {uialertview * alertview = [[uialertview alloc] initwithtitle: Nil message: @ "WiFi network" delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alertview show]; nslog (@ "Notification says wifinet") ;}}-(void) dealloc {// Delete the notification object [[nsnotifcenter center defaultcenter] removeobserver: Self];}-(void) applicationwillresignactive :( uiapplication *) application {// sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationdidenterbackground :( uiapplication *) Application {// use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // if your application supports background execution, this method is called instead of applicationwillterminate: when the user quits .} -(void) applicationwillenterforeground :( uiapplication *) Application {// called as part of the transition from the background to the inactive state; here you can undo records of the changes made on entering the background .} -(void) applicationdidbecomeactive :( uiapplication *) Application {// restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} -(void) applicationwillterminate :( uiapplication *) Application {// called when the application is about to terminate. save data if appropriate. see also applicationdidenterbackground :.}

Where to use:

-(Void) viewdidload {[Super viewdidload]; uibutton * BTN = [uibutton buttonwithtype: uibuttontypecontactadd]; BTN. center = cgpointmake (100,200); [BTN addtarget: Self action: @ selector (btnclick) forcontrolevents: uicontroleventtouchupinside]; [self. view addsubview: BTN];}-(void) btnclick {appdelegate * appdlg = (appdelegate *) [[uiapplication sharedapplication] Delegate]; nsparameterassert ([appdlg. reach iskindofclass: [reachability class]); networkstatus status = [appdlg. reach failed]; If (status = notreachable) {// uialertview * alertview = [[uialertview alloc] initwithtitle: Nil message: @ "Network disconnected" delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; // [alertview show]; nslog (@ "---- notification says unreachable");} else if (status = reachableviawwan) {// uialertview * alertview = [[uialertview alloc] initwithtitle: Nil message: @ "mobile network" delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; // [alertview show]; nslog (@ "---- notification says mobilenet");} else if (status = reachableviawifi) {// uialertview * alertview = [[uialertview alloc] initwithtitle: Nil message: @ "WiFi network" delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; // [alertview show]; nslog (@ "---- notification says wifinet ");}}


Note:

The network type can only be determined here. If you connect to Chinanet, it will also be displayed as WiFi available .. I do not know how to determine whether the Network is available.

Reachability is used to determine whether a network is available anywhere

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.