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