Giter VIP home page Giter VIP logo

eppz.reachability's Introduction

eppz!reachability

A pretty comfortable reachability class to make our life easier. Block-based on-demand implementaion won't scatter you code, nor block your main thread. It also works fine with IP addresses as well (!).

// Get status on-demand.
[EPPZReachability reachHost:hostNameOrIPaddress
                 completion:^(EPPZReachability *reachability)
{ if (reachability.reachable) [self postSomething]; }];

Also there is an option to observe reachability status without any need to reference any instance on client side.

// Listen.
[EPPZReachability listenHost:hostNameOrIPaddress delegate:self];

// Get notified.
-(void)reachabilityChanged:(EPPZReachability*) reachability
{
    if (reachability.reachableViaCellular) [self skipThumbnails];
}

Design

Network reachability is unfairly overmystyfied (at least it was for me) due to the really confusing sample implementation provided by Apple. Actually to ask for a particular hostname reachability information is three lines of code.

SCNetworkReachabilityRef reachabilityRef = SCNetworkReachabilityCreateWithName(NULL, [@"google.com" UTF8String]);
SCNetworkReachabilityFlags reachabilityFlags;
SCNetworkReachabilityGetFlags(reachabilityRef, &reachabilityFlags);

The rest is just implementation sugar. Three innocent lines of code. No notifications (which I instinctively don’t like), no run loops, no any weird stuff, like external setting an objects instance variables. Just the pure feature. The sample above uses a synchronous call I found in SCNetworkReachabiliy documentation called SCNetworkReachabilityGetFlags. So I built this implementation from the ground up to have a more Cocoa Reachability, and to get rid of any unnecessary stuff.

My personal favourite is that this implementation just works fine with IP addresses as well (this is the main reason I wrote this Nth reachability wrapper actually). I've struggeled with this issue for days, you can read more about it at Why asynchronous SCNetworkReachability not works with IP addresses?. So factory methods accepts both hostnames and IP addresses.

+(void)listenHost:(NSString*) hostNameOrAddress delegate:(id<EPPZReachabilityDelegate>) delegate; 
+(void)reachHost:(NSString*) hostNameOrAddress completion:(EPPZReachabilityCompletionBlock) completion;

As you may notice there are no any instances returned, since the class maintains a collection of EPPZReachability objects. I have not tested under every circumstances, so if you experience any misbehaviour please let me know by leaving a comment at the corresponding blog post Simplest Reachability ever (though, I'm gonna use it in production soon). Hope you like it as I do.

License

Licensed under the Open Source MIT license.

githalytics.com alpha Bitdeli Badge

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.