- -(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
-
- {
-
- Self. Window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease];
-
-
- // Enable Network Monitoring
-
- [[Nsicationicationcenter defacenter center] 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];
-
- ReturnYes;
-
- }
-
- // Method called when the network link changes
-
- -(Void) Reachabilitychanged :( nsnotification *) Note
-
- {
-
- Reachability * currreach = [Note object];
-
- Nsparameterassert ([currreach iskindofclass: [reachabilityClass]);
-
-
- // Respond to connection changes
- Networkstatus status = [currreach currentreachabilitystatus];
-
- // If the network is not connected, a notification is displayed.
-
- Self. isreachable = yes;
-
- If(Status = notreachable)
-
- {
- Uialertview * Alert = [[uialertview alloc] initwithtitle :@"Network connection exception"Message :@"No access to 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 is normal"Delegate: Nil cancelbuttontitle :@"OK"Otherbuttontitles: Nil];
-
- [Alert show];
-
- [Alert release];
-
- Self. isreachable = yes;
-
- }
-
- }
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelscreen]; // enable the network condition listener [[nsicationicationcenter defacenter center] addobserver: Self selector: @ selector (reachabilitychanged :) name: kreachabilitychangednotification object: Nil]; self. hostreach = [reachability reachabilitywithhostname: @ "www.baidu.com"]; [self. hostreach startnotifier]; // starts a run loop self. window. rootviewcontroller = self. tabbarcontroller; [self. window makekeyandvisible]; return yes;} // method called when the network link changes-(void) reachabilitychanged :( nsnotification *) Note {reachability * currreach = [Note object]; nsparameterassert ([currreach iskindofclass: [reachability class]); // response to connection changes networkstatus status = [currreach currentreachabilitystatus]; // if it is not connected to the network, a prompt self is displayed. isreachable = yes; If (status = notreachable) {uialertview * Alert = [[uialertview alloc] initwithtitle: @ "network connection exception" message: @ "no access to 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 normal" delegate: Nil cancelbuttontitle: @ "OK" otherbuttontitles: Nil]; [alert show]; [alert release]; self. isreachable = yes ;}}
UseCode, In the applicationProgramYou can use the following Singleton to determine whether the network is connected.
[CPP] View plaincopyprint?
- appdelegate * appdlg = (appdelegate *) [[uiapplication sharedapplication] Delegate];
- If (appdlg. isreachable)
- {
- nslog (@ "network connected" ); // code when the network is normal
- }
- else
- {
- nslog (@ "network connection exception" ); // code used to execute a network exception
- }