Adding Systemconfiguration.framework to the project
The corresponding. h file
#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller//If the method is preceded by +, it is equivalent to the static method of the class, note here-(BOOL) Connectedtonetwork; @end
The corresponding. m file
#import "ViewController.h" #import <Foundation/Foundation.h> #import <CommonCrypto/CommonHMAC.h> #import <SystemConfiguration/SystemConfiguration.h> #import <netdb.h> #import <arpa/inet.h> @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; UIButton *btn = [[UIButton alloc]init]; Btn.frame = CGRectMake (20, 20, 40, 40); Btn.backgroundcolor = [Uicolor Browncolor]; [Self.view ADDSUBVIEW:BTN]; [Btn addtarget:self Action: @selector (testout:) forcontrolevents:uicontroleventtouchupinside]; Do any additional setup after loading the view, typically from a nib.} -(BOOL) connectedtonetwork{//Create Zero addy struct sockaddr_in zeroaddress; Bzero (&zeroaddress, sizeof (zeroaddress)); Zeroaddress.sin_len = sizeof (zeroaddress); zeroaddress.sin_family = af_inet; Recover reachability flags Scnetworkreachabilityref defaultroutereachability = ScnetworkreachabilitycreatewithaDdress (NULL, (struct sockaddr *) &zeroaddress); Scnetworkreachabilityflags flags; BOOL didretrieveflags = Scnetworkreachabilitygetflags (defaultroutereachability, &flags); Cfrelease (defaultroutereachability); if (!didretrieveflags) {printf ("Error. Could not recover network reachability flags\n "); return NO; } BOOL isreachable = ((Flags & kscnetworkflagsreachable)! = 0); BOOL needsconnection = ((Flags & kscnetworkflagsconnectionrequired)! = 0); Return (isreachable &&!needsconnection)? Yes:no;} At the time of the call, add the judgment in the corresponding button:-(ibaction) Testout: (UIButton *) sender{if (![ Self connectedtonetwork]) {Uialertview *alert=[[uialertview alloc] initwithtitle:@ "Warm tip" message:@ "Network connection failed, please See if the network is connected properly! "Delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil]; [Alert show]; } NSLog (@ "Testout Event");} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose ofAny resources which can be recreated.} @end
IOS Judging network Status Simple example