In iOS, Reachability is used to detect the network type of the mobile phone. Wi-Fi and 2 3 4G, iosreachability

Source: Internet
Author: User

In iOS, Reachability is used to detect the network type of the mobile phone. Wi-Fi and 2 3 4G, iosreachability

If you want to provide a Reeder In the iOS program, or provide offline mode (Evernote) in the absence of a network ). Then you will use the Reachability for network detection.

 

Purpose of writing this article
  • Understand what Reachability can do
  • Detection 3 network environment
    • 2G/3G
    • Wifi
    • No network
  • How to Use notifications
    • Single controller
    • Multiple Controllers
  • Simple functions:
    • Use only in wifi
Reachability Overview

Reachablity is a library used for iOS device network environment detection.

  • Monitor whether the target network is available
  • Monitor the connection mode of the current network
  • Monitor connection mode changes

Doc officially provided by Apple

Http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

 

Github documentation

Https://github.com/tonymillion/Reachability

 

Install

Because Reachability is very common. Add it directly to Supporting Files/networ-Prefix.pch:

 

C code
  1. # Import <Reachability/Reachability. h>

 

If you do not know what cocoaspod is, check the following:

Http://witcheryne.iteye.com/blog/1873221

 

Use

There is an answer in stackoverflow to explain the reachability usage.

Http://stackoverflow.com/questions/11177066/how-to-use-ios-reachability

  • Generally, a Reachability instance is OK.
  • Only one Reachability is required for a Controller.
Block

 

C code
  1. -(Void) viewDidLoad
  2. {
  3. [Super viewDidLoad];
  4. DLog (@ "enable www.apple.com network detection ");
  5. Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"];
  6. DLog (@ "-- current status: % @", reach. currentReachabilityString );
  7. // Start the notifier which will cause the reachability object to retain itself!
  8. [[Nsicationcenter center defacenter center] addObserver: self
  9. Selector: @ selector (reachabilityChanged :)
  10. Name: kReachabilityChangedNotification
  11. Object: nil];
  12. Reach. reachableBlock = ^ (Reachability * reachability)
  13. {
  14. Dispatch_async (dispatch_get_main_queue (), ^ {
  15. Self. blockLabel. text = @ "network available ";
  16. Self. blockLabel. backgroundColor = [UIColor greenColor];
  17. });
  18. };
  19. Reach. unreachableBlock = ^ (Reachability * reachability)
  20. {
  21. Dispatch_async (dispatch_get_main_queue (), ^ {
  22. Self. blockLabel. text = @ "Network unavailable ";
  23. Self. blockLabel. backgroundColor = [UIColor redColor];
  24. });
  25. };
  26. [Reach startNotifier];
  27. }

 

 

Notification

 

C code
  1. -(Void) viewDidLoad
  2. {
  3. [Super viewDidLoad];
  4. DLog (@ "enable www.apple.com network detection ");
  5. Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"];
  6. DLog (@ "-- current status: % @", reach. currentReachabilityString );
  7. // Start the notifier which will cause the reachability object to retain itself!
  8. [[Nsicationcenter center defacenter center] addObserver: self
  9. Selector: @ selector (reachabilityChanged :)
  10. Name: kReachabilityChangedNotification
  11. Object: nil];
  12. [Reach startNotifier];
  13. }
  14. -(Void) reachabilityChanged: (NSNotification *) note {
  15. Reachability * reach = [note object];
  16. If (! [Reach isReachable])
  17. {
  18. Self. icationicationlabel. text = @ "Network unavailable ";
  19. Self. icationicationlabel. backgroundColor = [UIColor redColor];
  20. Self. wifiOnlyLabel. backgroundColor = [UIColor redColor];
  21. Self. wwanOnlyLabel. backgroundColor = [UIColor redColor];
  22. Return;
  23. }
  24. Self. icationicationlabel. text = @ "network available ";
  25. Self. icationicationlabel. backgroundColor = [UIColor greenColor];
  26. If (reach. isReachableViaWiFi ){
  27. Self. wifiOnlyLabel. backgroundColor = [UIColor greenColor];
  28. Self. wifiOnlyLabel. text = @ "currently connected via wifi ";
  29. } Else {
  30. Self. wifiOnlyLabel. backgroundColor = [UIColor redColor];
  31. Self. wifiOnlyLabel. text = @ "wifi is not enabled and cannot be used ";
  32. }
  33. If (reach. isReachableViaWWAN ){
  34. Self. wwanOnlyLabel. backgroundColor = [UIColor greenColor];
  35. Self. wwanOnlyLabel. text = @ "currently connected through 2g or 3g ";
  36. } Else {
  37. Self. wwanOnlyLabel. backgroundColor = [UIColor redColor];
  38. Self. wwanOnlyLabel. text = @ "2g or 3g network not used ";
  39. }
  40. }

 

 

Appendix demo description enable wifi


Disable wifi status


 

 

 

Legacy problems

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.