IOS Judging network Status Simple example

Source: Internet
Author: User

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

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.