Git Product home page Git Product logo

reachability-ios's Introduction

Network reachability for iOS and OS X

About

SCNetworkReachability class is the wrapper on C-structures and C-functions of SCNetworkReachability API in SystemConfiguration.framework

Differences from Apple's reachability
  • No NSNotificationCenter. Use callback blocks to determine reachability status or observe changes
  • No synchronous reachability checks. Your app will not crash because of bad connection
  • No "code smell". All code is in OOP-style.
  • OS X support

Installation

Add pod 'SCNetworkReachability' to Podfile

Using

Check reachability status

Check current reachability status of some host. This callback will run only once immediately after reachability status will be determined. Note. Callback block will run on main queue!

SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initWithHost:@"https://github.com"];
[reachability reachabilityStatus:^(SCNetworkStatus status)
{
    switch (status)
    {
        case SCNetworkStatusReachableViaWiFi:
            NSLog(@"Reachable via WiFi");
            break;

        case SCNetworkStatusReachableViaCellular:
            NSLog(@"Reachable via Cellular");
            break;

        case SCNetworkStatusNotReachable:
            NSLog(@"Not Reachable");
            break;
    }
}];

And if you don't want to store instance of SCNetworkReachability you can do the same with class method.

[SCNetworkReachability host:@"github.com" reachabilityStatus:^(SCNetworkStatus status)
{
    switch (status)
    {
        case SCNetworkStatusReachableViaWiFi:
            NSLog(@"Reachable via WiFi");
            break;

        case SCNetworkStatusReachableViaCellular:
            NSLog(@"Reachable via Cellular");
            break;

        case SCNetworkStatusNotReachable:
            NSLog(@"Not Reachable");
            break;
    }
}];

Note. There is only one difference from iOS that you have only one status SCNetworkStatusReachable instead of SCNetworkStatusReachableViaWiFi and SCNetworkStatusReachableViaCellular.

Observe reachability changes

Observe reachability changes of some host. This callback will run each time reachability status will be changed.

SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initWithHost:@"example.com"];
[reachability observeReachability:^(SCNetworkStatus status)
{
    switch (status)
    {
        case SCNetworkStatusReachableViaWiFi:
            NSLog(@"Reachable via WiFi");
            break;

        case SCNetworkStatusReachableViaCellular:
            NSLog(@"Reachable via Cellular");
            break;

        case SCNetworkStatusNotReachable:
            NSLog(@"Not Reachable");
            break;
    }
}];

Multithreading

Sometimes you need to check reachability not on main queue. And it's easy with Multithreading subspec.

Installation

Add pod 'SCNetworkReachability/Multithreading' to Podfile.

Using

Multithreading subspec allows to set custom dispatch queue for callbacks. So you can choose any of this methods.

- (void)observeReachabilityOnDispatchQueue:(dispatch_queue_t)queue
                                  callback:(void (^)(SCNetworkStatus))block;
- (void)reachabilityStatusOnDispatchQueue:(dispatch_queue_t)queue
                                 callback:(void (^)(SCNetworkStatus))block;
+ (void)hostReachabilityStatus:(NSString *)host onDispatchQueue:(dispatch_queue_t)queue
                      callback:(void (^)(SCNetworkStatus))block;

Shared reachability

Another useful subspec is Shared. It creates shared instance of SCNetworkReachability class.

Installation

Add pod 'SCNetworkReachability/Shared' to Podfile.

Using
SCNetworkReachability *reachability = SCNetworkReachability.shared;

Compatibility with Apple's reachability

I've leaved two reachability initialisation methods to make it compatible with Apple's reachabililty. I've never used them but it can be useful for someone.

Installation

Add pod 'SCNetworkReachability/Compatibility' to Podfile.

Using

Determine WiFi reachability on device. Available only on iOS.

SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initForLocalWiFi];

Reachability with IP-address struct

struct sockaddr_in address;
// Does anybody knows how to define this structure? =)
address = ...
SCNetworkReachability *reachability = [[SCNetworkReachability alloc] initWithHostAddress:&address];

History

Releases

Updates

Follow updates on twitter @okolodev

reachability-ios's People

Contributors

belkevich avatar bonethings avatar

Stargazers

Mark Smith avatar 0IHI0 avatar  avatar isaiah Calderon avatar jianbing li avatar empty space avatar OddyCode avatar Vlad Korzun avatar Pulkit Sood avatar Eran Guttentag avatar boomstack avatar Taha Dhiaeddine Amdouni avatar Liu Yi avatar Pei Qing 卿培 avatar  avatar Tiago Braga avatar Arn avatar Kai Li avatar Gabe Rozenberg avatar Jeff Day avatar Sam Bateman avatar  avatar Sutra Zhou avatar  avatar Eleven Chen avatar Andy avatar Mr Tolstoy avatar  avatar Brad Folkens avatar EA88 avatar Mateusz "Serafin" Gajewski avatar Mateusz Charytoniuk avatar Alexander Lingtorp avatar Han avatar Vivek Pandya avatar Benjamin Graner avatar  avatar kalloc avatar Ram avatar kimziv avatar Michael avatar Dimo Hamdy avatar Pierre Rougeot avatar Sergii Kryvoblotskyi avatar Herbert Siojo avatar Minho Ryang avatar Alex avatar John Dunne avatar  avatar  avatar Dmitry Golomidov avatar  avatar Rod Garcia avatar Hasan Yasin Türkyılmaz avatar SirDyf avatar  avatar Ignacio Romero Zurbuchen avatar  avatar  avatar Geoffroy Lesage avatar mayulu avatar Karl Diao avatar AlexDenisov avatar Yan Li avatar Denys Telezhkin avatar Marcin avatar Simon Fortelny avatar Nick Peelman avatar  avatar

Watchers

Klaus avatar mayulu avatar John Dunne avatar Geoffroy Lesage avatar Arn avatar AlexDenisov avatar  avatar Hasan Yasin Türkyılmaz avatar Sameh Farouk avatar

reachability-ios's Issues

CocoaPods

Can you provide CocoaPods support?

Thanks for your good work.

Not working with Release Configuration

It seems fine when developing with SCNetworkReachability. But when switching to Release Configuration, SCNetworkReachability.status is always zero(SCNetworkStatusNotReachable).

Apparently, [SCNetworkReachabilityFlagsParser checkReachabilityRefFlags:reachabilityRef] always return NO if DEBUG is not defined.

Opposite status reported in simulator

Is it expected that self.network.observeReachability (where self.network = SCNetworkReachability(host: "http://......")) seems to always report the opposite status (unreachable when reachable, and vice versa) when running in iOS simulator (iOS 8.3)? It seems to work fine when deployed to the actual device.

Cannot find protocol declaration for 'SCNetworkReachabilityDelegate'

Hi,

I am using your library with Cocoapods. Unfortunately I get some error messages when trying to run this code.

/Users/david/Desktop/Zeugs/Objective-C Zeugs/iOSApps/PinFever/Pods/ABRequestManager/Classes/Managers/ABRequestManager.m:20:33: Cannot find protocol declaration for 'SCNetworkReachabilityDelegate'
/Users/david/Desktop/Zeugs/Objective-C Zeugs/iOSApps/PinFever/Pods/ABRequestManager/Classes/Managers/ABRequestManager.m:102:56: No visible @interface for 'SCNetworkReachability' declares the selector 'initWithHostName:'
/Users/david/Desktop/Zeugs/Objective-C Zeugs/iOSApps/PinFever/Pods/ABRequestManager/Classes/Managers/ABRequestManager.m:103:23: Property 'delegate' not found on object of type 'SCNetworkReachability *'

Seems like a version error to me since there is no delegate in the SCNetworkReachability class I get from pod install.

Using 2.0.0 Reachability Status reports incorrectly

Tested in 1.2.0 and verified as working.
Tested in 2.0.0 and verified as broken.

To reproduce on an iOS 7 device.

  • Monitor reachability changes
  • Put phone into Airplane Mode

Expected:

  • Device reports its reachability status as Unreachable

Actual:

  • Device reports its reachability status as Unreachable
  • Then after a few seconds reports as Reachable via WWAN.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.