IOS Off-screen processing(2014-01-13 18:13:21)
reproduced
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
Turn on network condition monitoring
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];
Self.hostreach = [reachability reachabilitywithhostname:@ "www.baidu.com"];
[Self.hostreach Startnotifier]; Start listening, a run loop is started
Self.window.rootViewController = Self.tabbarcontroller;
[Self.window makekeyandvisible];
return YES;
}
The method that will be called when the network link changes
-(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:@ "temporarily unable to access bookstore information" Delegate:nil Cancelbuttontitle: @ "OK" otherbuttontitles:nil];
[Alert show];
[Alert release];
self.isreachable = NO;
}
Else
{
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;
}
}
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
Turn on network condition monitoring
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];
Self.hostreach = [reachability reachabilitywithhostname:@ "www.baidu.com"];
[Self.hostreach Startnotifier]; Start listening, a run loop is started
Self.window.rootViewController = Self.tabbarcontroller;
[Self.window makekeyandvisible];
return YES;
}
The method that will be called when the network link changes
-(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:@ "temporarily unable to access bookstore information" Delegate:nil Cancelbuttontitle: @ "OK" otherbuttontitles:nil];
[Alert show];
[Alert release];
self.isreachable = NO;
}
Else
{
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;
}
}
Using the code above, you can use the following singleton to determine whether the network is connected in any one of the application's interfaces
[CPP] View plaincopyprint?
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
}
IOS Off-screen processing