Giter Site home page Giter Site logo

sdreachability's Introduction

SDReachability

SDReachability is a lightweight rewrite of Apple's Reachability example library focused on easy embedability and usage simplicity.

Most network based library needs Reachability feature but can't embed the Apple implementation without taking the risk to name class with the same library embeded by another library or the hosted app itself. SDReachability solve this issue by letting embeders easily modify symbols exported by the library by adding their own prefix. Additionnaly, SDReachability doesn't relies on global NSNotification but uses target/action pattern.

SDReachability API is revisited in order to be simpler to use. You just have to instanciate it and give it a target object with an action selector. You must store the resulting instance. You don't have to start/stop the notif(i)er, it's started immediatly after initialization and is stopped as soon as its instance is deallocated โ€” which should happen when the host object will be deallocated itself.

Installation

  • Copy the .h/.m files into your project
  • In you application project application's target settings, find the "Build Phases" section and open the "Link Binary With Libraries" block
  • Click the "+" button again and select the SystemConfiguration.framework

Usage

Check if Internet is reachable:

SDReachability *reach = SDReachability.new;
if (reach.isReachable)
{
    NSLog(@"Connected with %@", reach.isReachableViaWWAN ? @"3G" : @"WiFi");
}
else
{
    NSLog(@"Not Connected");
}

Subscribe to connectivity changes

@interface MyObject ()

@property (strong, nonatomic) SDReachability *reachability;

@end


@implementation MyObject

- (void)monitorReachability
{
    self.reachability = [SDReachability reachabilityWithTarget:self action:@selector(reachabilityChanged:)];
}

- (void)reachabilityChanged:(SDReachability *)reachability
{
    switch (reachability.reachabilityStatus)
    {
        case SDNotReachable:
            NSLog(@"Connection lost");
            break;

        case SDReachableViaWiFi:
            NSLog(@"Connected via WiFi");
            break;

        case SDReachableViaWWAN:
            NSLog(@"Connected via WWAN");
            break;
    }
}

@end

Embedding

If you want to distribute a static library with reachability support, it's a bad idea to just copy the Apple provided Reachability demo class. Chances are that your users will already use this class either directly or through another library, leading to name claching and compilation headache.

SDReachability helps you with embedding by providing an easy way to rename all exported symbols so they won't clash. To embed SDReachability into your project, copy the .h and .m files and modify the #define instructions at the top of the .h file like this:

#undef $SDReachability
#define $SDReachability MyLibraryReachability
#undef $SDReachabilityStatus
#define $SDReachabilityStatus MyLibraryReachabilityStatus
#undef $SDNotReachable
#define $SDNotReachable MyLibraryNotReachable
#undef $SDReachableViaWiFi
#define $SDReachableViaWiFi MyLibraryReachableViaWiFi
#undef $SDReachableViaWWAN
#define $SDReachableViaWWAN MyLibraryReachableViaWWAN

In your library, use MyLibraryReachability instead of SDReachability, same for all other redefined symboles.

License

All source code is licensed under the MIT License.

sdreachability's People

Watchers

James Cloos avatar  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.