Network listening for iOS

Source: Internet
Author: User
  1. -(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
  2. {
  3. Self. Window = [[[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease];
  4. // Enable Network Monitoring
  5. [[Nsicationicationcenter defacenter center] addobserver: Self selector: @ selector (reachabilitychanged :) name: kreachabilitychangednotification object: Nil];
  6. Self. hostreach = [reachability reachabilitywithhostname :@"Www.baidu.com"];
  7. [Self. hostreach startnotifier];// Start listening. A run loop is started.
  8. Self. Window. rootviewcontroller = self. tabbarcontroller;
  9. [Self. Window makekeyandvisible];
  10. ReturnYes;
  11. }
  12. // Method called when the network link changes
  13. -(Void) Reachabilitychanged :( nsnotification *) Note
  14. {
  15. Reachability * currreach = [Note object];
  16. Nsparameterassert ([currreach iskindofclass: [reachabilityClass]);
  17. // Respond to connection changes
  18. Networkstatus status = [currreach currentreachabilitystatus];
  19. // If the network is not connected, a notification is displayed.
  20. Self. isreachable = yes;
  21. If(Status = notreachable)
  22. {
  23. Uialertview * Alert = [[uialertview alloc] initwithtitle :@"Network connection exception"Message :@"No access to bookstore information"Delegate: Nil cancelbuttontitle :@"OK"Otherbuttontitles: Nil];
  24. [Alert show];
  25. [Alert release];
  26. Self. isreachable = no;
  27. }
  28. Else
  29. {
  30. Uialertview * Alert = [[uialertview alloc] initwithtitle :@"Network connection information"Message :@"Network connection is normal"Delegate: Nil cancelbuttontitle :@"OK"Otherbuttontitles: Nil];
  31. [Alert show];
  32. [Alert release];
  33. Self. isreachable = yes;
  34. }
  35. }
-(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?
    1. appdelegate * appdlg = (appdelegate *) [[uiapplication sharedapplication] Delegate];
    2. If (appdlg. isreachable)
    3. {
    4. nslog (@ "network connected" ); // code when the network is normal
    5. }
    6. else
    7. {
    8. nslog (@ "network connection exception" ); // code used to execute a network exception
    9. }
Related Article

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.