Use Reachability in iOS to detect network and iosreachability

Source: Internet
Author: User

Use Reachability in iOS to detect network and iosreachability
Use Reachability in iOS to detect NetworksContent prompt:Offline mode (Evernote) is provided ). Then you will use the Reachability for network detection. The purpose of this article is to understand what Reachability can do to detect the network environment in 3 2G/3G wifi no network how to use the simple function of notifying multiple controllers in a single controller: use Reachability only under wifi introduction Reachablity is a iOS...

 

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:

 

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

 

1234567891011121314151617181920212223242526272829303132 - (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!          [[NSNotificationCenter defaultCenter] 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

 

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 - (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!          [[NSNotificationCenter defaultCenter] addObserver:self                                                        selector:@selector(reachabilityChanged:)                                                             name:kReachabilityChangedNotification                                                          object:nil];     [reach startNotifier];} - (void) reachabilityChanged: (NSNotification*)note {     Reachability * reach = [note object];        if(![reach isReachable])    {        self.notificationLabel.text = @"Network unavailable";          self.notificationLabel.backgroundColor = [UIColor redColor];          self.wifiOnlyLabel.backgroundColor = [UIColor redColor];          self.wwanOnlyLabel.backgroundColor = [UIColor redColor];          return;    }             self.notificationLabel.text = @"Network available";     self.notificationLabel.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";     }}

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.