Use Reachability in iOS to detect network and iosreachability

Source: Internet
Author: User

Use Reachability in iOS to detect network and 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
  • # Import <Reachability/Reachability. h>
  • #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
  • -(Void) viewDidLoad
  • {
  • [Super viewDidLoad];
  • DLog (@ "enable www.apple.com network detection ");
  • Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"];
  • DLog (@ "-- current status: % @", reach. currentReachabilityString );
  • // Start the notifier which will cause the reachability object to retain itself!
  • [[Nsicationcenter center defacenter center] addObserver: self
  • Selector: @ selector (reachabilityChanged :)
  • Name: kReachabilityChangedNotification
  • Object: nil];
  • Reach. reachableBlock = ^ (Reachability * reachability)
  • {
  • Dispatch_async (dispatch_get_main_queue (), ^ {
  • Self. blockLabel. text = @ "network available ";
  • Self. blockLabel. backgroundColor = [UIColor greenColor];
  • });
  • };
  • Reach. unreachableBlock = ^ (Reachability * reachability)
  • {
  • Dispatch_async (dispatch_get_main_queue (), ^ {
  • Self. blockLabel. text = @ "Network unavailable ";
  • Self. blockLabel. backgroundColor = [UIColor redColor];
  • });
  • };
  • [Reach startNotifier];
  • }
  • -(Void) viewDidLoad {[super viewDidLoad]; DLog (@ "enable www.apple.com network detection"); Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"]; DLog (@ "-- current status: % @", reach. currentReachabilityString); // start the notifier which will cause the reachability object to retain itself! [[Nsicationicationcenter defacenter center] addObserver: self selector: @ selector (reachabilityChanged :) name: kReachabilityChangedNotification object: nil]; reach. reachableBlock = ^ (Reachability * reachability) {dispatch_async (dispatch_get_main_queue (), ^ {self. blockLabel. text = @ "network available"; self. blockLabel. backgroundColor = [UIColor greenColor] ;}) ;}; reach. unreachableBlock = ^ (Reachability * reachability) {dispatch_async (dispatch_get_main_queue (), ^ {self. blockLabel. text = @ "Network unavailable"; self. blockLabel. backgroundColor = [UIColor redColor] ;}; [reach startNotifier];}

     

     

    Notification

     

    C code
  • -(Void) viewDidLoad
  • {
  • [Super viewDidLoad];
  • DLog (@ "enable www.apple.com network detection ");
  • Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"];
  • DLog (@ "-- current status: % @", reach. currentReachabilityString );
  • // Start the notifier which will cause the reachability object to retain itself!
  • [[Nsicationcenter center defacenter center] addObserver: self
  • Selector: @ selector (reachabilityChanged :)
  • Name: kReachabilityChangedNotification
  • Object: nil];
  • [Reach startNotifier];
  • }
  • -(Void) reachabilityChanged: (NSNotification *) note {
  • Reachability * reach = [note object];
  • If (! [Reach isReachable])
  • {
  • Self. icationicationlabel. text = @ "Network unavailable ";
  • Self. icationicationlabel. backgroundColor = [UIColor redColor];
  • Self. wifiOnlyLabel. backgroundColor = [UIColor redColor];
  • Self. wwanOnlyLabel. backgroundColor = [UIColor redColor];
  • Return;
  • }
  • Self. icationicationlabel. text = @ "network available ";
  • Self. icationicationlabel. backgroundColor = [UIColor greenColor];
  • If (reach. isReachableViaWiFi ){
  • Self. wifiOnlyLabel. backgroundColor = [UIColor greenColor];
  • Self. wifiOnlyLabel. text = @ "currently connected via wifi ";
  • } Else {
  • Self. wifiOnlyLabel. backgroundColor = [UIColor redColor];
  • Self. wifiOnlyLabel. text = @ "wifi is not enabled and cannot be used ";
  • }
  • If (reach. isReachableViaWWAN ){
  • Self. wwanOnlyLabel. backgroundColor = [UIColor greenColor];
  • Self. wwanOnlyLabel. text = @ "currently connected through 2g or 3g ";
  • } Else {
  • Self. wwanOnlyLabel. backgroundColor = [UIColor redColor];
  • Self. wwanOnlyLabel. text = @ "2g or 3g network not used ";
  • }
  • }
  • -(Void) viewDidLoad {[super viewDidLoad]; DLog (@ "enable www.apple.com network detection"); Reachability * reach = [Reachability reachabilityWithHostname: @ "www.apple.com"]; DLog (@ "-- current status: % @", reach. currentReachabilityString); // start the notifier which will cause the reachability object to retain itself! [[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (reachabilityChanged :) name: jsonobject: nil]; [reach startNotifier];}-(void) reachabilityChanged: (NSNotification *) note {Reachability * reach = [note object]; if (! [Reach isReachable]) {self. icationicationlabel. text = @ "Network unavailable"; self. icationicationlabel. backgroundColor = [UIColor redColor]; self. wifiOnlyLabel. backgroundColor = [UIColor redColor]; self. wwanOnlyLabel. backgroundColor = [UIColor redColor]; return;} self. icationicationlabel. text = @ "network available"; self. icationicationlabel. backgroundColor = [UIColor greenColor]; if (reach. isReachableViaWiFi) {self. wifiOnlyLabel. backgroundColor = [UIColor greenColor]; self. wifiOnlyLabel. text = @ "currently connected via wifi";} else {self. wifiOnlyLabel. backgroundColor = [UIColor redColor]; self. wifiOnlyLabel. text = @ "wifi not enabled, not available";} if (reach. isReachableViaWWAN) {self. wwanOnlyLabel. backgroundColor = [UIColor greenColor]; self. wwanOnlyLabel. text = @ "currently connected through 2g or 3g";} else {self. wwanOnlyLabel. backgroundColor = [UIColor redColor]; self. wwanOnlyLabel. text = @ "2g or 3g network not used ";}}

     

     

    Appendix demo description enable wifi

    Disable wifi status

     

     

    Legacy problems
    Ios development determines whether the network is enabled

    Go to the official website to download his demo. Reachability. Here, Reachability. h and Reachability. m are imported to your project. Hope to adopt
     
    IOS oc checks whether mobile network and Wi-Fi are enabled

    Use currentReachabilityStatus, A Reachability method, to obtain the enumerated type value and determine whether it is 2. If it is not 2, it is not WiFi.

    Enum {

    // DDG NetworkStatus Constant Names.
    KNotReachable = 0, // Apple's code depends upon 'notreachable' being the same value as 'no '.
    KReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
    KReachableViaWiFi

    };
    Typedef uint32_t NetworkStatus;

    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.