Use reachability detection network in iOS to differentiate cellular type WiFi and 2 3 4 G

Source: Internet
Author: User

If you want to provide an iOS program only for use under WiFi network (Reeder), or to provide offline mode (Evernote) in the absence of network status. Then you will use the reachability to implement network detection.

The purpose of writing this article
    • Learn what reachability can do
    • Detecting the network environment in 3
      • 2g/3g
      • Wifi
      • No network
    • How to use Notifications
      • Single Controller
      • Multiple controllers
    • Simple features:
      • Use only under WiFi
reachability Introduction

Reachablity is a library of iOS device network environments that detects iOS devices.

    • Monitoring whether the target network is available
    • Monitor how your current network is connected
    • Monitoring changes in connection mode

The official doc provided by Apple

http://developer.apple.com/library/ios/#samplecode/reachability/introduction/intro.html

Documentation on GitHub

Https://github.com/tonymillion/Reachability

Installation
    1. Create a network project (network is the demo project I created, which can be downloaded in the attachment)
    2. Installing dependencies using Cocoaspod
    3. Add a systemconfiguration.framework library to your project

Because reachability is very common. Add it directly to the supporting FILES/NETWOR-PREFIX.PCH:

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

If you don't yet know what cocoaspod is, look here:

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

Use

There is an answer on the StackOverflow, which explains the use of reachability very well.

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

    • In general, a reachability instance is ok.
    • A controller needs only one reachability.
Block mode use

C code
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. DLog (@"Turn on www.apple.com Network Detection");
  5. reachability* reach = [reachability reachabilitywithhostname:@"www.apple.com"];
  6. DLog (@"--Current status:%@", reach.currentreachabilitystring);
  7. //Start the notifier which would cause the Reachability object to retain itself!
  8. [[Nsnotificationcenter Defaultcenter] 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. }

How to use notification

C code
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. DLog (@"Turn on www.apple.com Network Detection");
  5. reachability* reach = [reachability reachabilitywithhostname:@"www.apple.com"];
  6. DLog (@"--Current status:%@", reach.currentreachabilitystring);
  7. //Start the notifier which would cause the Reachability object to retain itself!
  8. [[Nsnotificationcenter Defaultcenter] 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.notificationLabel.text = @"Network unavailable";
  19. Self.notificationLabel.backgroundColor = [Uicolor Redcolor];
  20. Self.wifiOnlyLabel.backgroundColor = [Uicolor Redcolor];
  21. Self.wwanOnlyLabel.backgroundColor = [Uicolor Redcolor];
  22. return;
  23. }
  24. Self.notificationLabel.text = @"network available";
  25. Self.notificationLabel.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 not turned on, not available";
  32. }
  33. if (Reach.isreachableviawwan) {
  34. Self.wwanOnlyLabel.backgroundColor = [Uicolor Greencolor];
  35. Self.wwanOnlyLabel.text = @"Currently connected via 2g or 3g";
  36. } Else {
  37. Self.wwanOnlyLabel.backgroundColor = [Uicolor Redcolor];
  38. Self.wwanOnlyLabel.text = @"2g or 3g network not in use";
  39. }
  40. }

Accessories demo instructions turn on WiFi status


Turn off the WiFi status


Legacy issues
      1. How to share a reachability before multiple controllers (attachment demo is a controller one reachability instance)
      2. What should be used to stop the detection of reachability.

Use reachability detection network in iOS to differentiate cellular type WiFi and 2 3 4 G

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.