Giter Site home page Giter Site 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];

Updates

Follow updates on twitter @okolodev

reachability-ios's People

Contributors

belkevich avatar

Watchers

James Cloos avatar Onur Unal avatar

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.