Use of reachability

Source: Internet
Author: User

Just to a new company to do a new project on the network status of monitoring and colleagues have produced a different view of my network monitoring is written by myself later found that they are not general silly there is a thing called reachability is very simple and practical very violent below is how to use

First, add the header file "Reachability.h" to the AppDelegate.h, and import the framework Systemconfiguration.frame

Here's the code:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions

{

Turn on network condition monitoring

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];

Self.hostreach = [reachability reachabilitywithhostname:@ "www.baidu.com"];

Start listening, a run loop is started

[Self.hostreach Startnotifier];

}

-(void) reachabilitychanged: (nsnotification *) Note

{

reachability *currreach = [Note Object];

Nsparameterassert ([Currreach iskindofclass:[reachability class]]);

Respond to changes in connection to process actions

NetworkStatus status = [Currreach currentreachabilitystatus];

Pop-up reminder if not connected to the network

self.isreachable = YES;

if (status = = Notreachable)

{

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Network connection exception" Message:nil delegate:nil cancelbuttontitle:@ "OK" Otherbuttontitles:nil];

[Alert show];

[Alert release];

self.isreachable = NO;

Return

}

if (status==kreachableviawifi| | Status==kreachableviawwan) {

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Network connection Information" message:@ "Network connection OK" Delegate:nil cancelbuttontitle:@ " Determine "Otherbuttontitles:nil";

[Alert show];

[Alert release];

self.isreachable = YES;

}

}

Then on each page of the viewwillappear: plus:

-(void) Viewwillappear: (BOOL) animated

{

[Super Viewwillappear:yes];

Appdelegate *appdlg = (appdelegate *) [[uiapplication sharedapplication] delegate];

if (appdlg.isreachable)

{

NSLog (@ "Network is connected");//code to perform network uptime

}

Else

{

NSLog (@ "Network connection exception");//code to execute network exception

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Network connection exception" Message:nil delegate:nil cancelbuttontitle:@ "OK" Otherbuttontitles:nil];

[Alert show];

[Alert release];

}

}

This allows you to check for sudden network outages and connections when you run the program.

OK is it not so NB

Use of reachability

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.