GithubHelp home page GithubHelp logo

bountylabs / reachability Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tonymillion/reachability

0.0 95.0 0.0 726 KB

ARC and GCD Compatible Reachability Class for iOS and MacOS. Drop in replacement for Apple Reachability

License: BSD 2-Clause "Simplified" License

Objective-C 93.96% Ruby 6.04%

reachability's Introduction

Reference Status

WARNING there have been reports of apps being rejected when Reachability is used in a framework. The only solution to this so far is to rename the class.

Reachability

This is a drop-in replacement for Apple's Reachability class. It is ARC-compatible, and it uses the new GCD methods to notify of network interface changes.

In addition to the standard NSNotification, it supports the use of blocks for when the network becomes reachable and unreachable.

Finally, you can specify whether a WWAN connection is considered "reachable".

DO NOT OPEN BUGS UNTIL YOU HAVE TESTED ON DEVICE

BEFORE YOU OPEN A BUG ABOUT iOS6/iOS5 build errors, use Tag 3.2 or 3.1 as they support assign types

Requirements

Once you have added the .h/m files to your project, simply:

  • Go to the Project->TARGETS->Build Phases->Link Binary With Libraries.
  • Press the plus in the lower left of the list.
  • Add SystemConfiguration.framework.

Boom, you're done.

Examples

Block Example

This sample uses blocks to notify when the interface state has changed. The blocks will be called on a BACKGROUND THREAD, so you need to dispatch UI updates onto the main thread.

In Objective-C

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
	// keep in mind this is called on a background thread
	// and if you are updating the UI it needs to happen
	// on the main thread, like this:

	dispatch_async(dispatch_get_main_queue(), ^{
	  NSLog(@"REACHABLE!");
	});
};

reach.unreachableBlock = ^(Reachability*reach)
{
	NSLog(@"UNREACHABLE!");
};

// Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];

In Swift

import Reachability

var reach: Reachability?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Allocate a reachability object
        self.reach = Reachability.reachabilityForInternetConnection()
        
        // Set the blocks
        self.reach!.reachableBlock = {
            (let reach: Reachability!) -> Void in
            
            // keep in mind this is called on a background thread
            // and if you are updating the UI it needs to happen
            // on the main thread, like this:
            dispatch_async(dispatch_get_main_queue()) {
                println("REACHABLE!")
            }
        }
        
        self.reach!.unreachableBlock = {
            (let reach: Reachability!) -> Void in
            println("UNREACHABLE!")
        }
        
        self.reach!.startNotifier()
	
        return true
}

NSNotification Example

This sample will use NSNotifications to notify when the interface has changed. They will be delivered on the MAIN THREAD, so you can do UI updates from within the function.

In addition, it asks the Reachability object to consider the WWAN (3G/EDGE/CDMA) as a non-reachable connection (you might use this if you are writing a video streaming app, for example, to save the user's data plan).

In Objective-C

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = NO;

// Here we set up a NSNotification observer. The Reachability that caused the notification
// is passed in the object parameter
[[NSNotificationCenter defaultCenter] addObserver:self
										 selector:@selector(reachabilityChanged:)
											 name:kReachabilityChangedNotification
										   object:nil];

[reach startNotifier];

In Swift

import Reachability

var reach: Reachability?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Allocate a reachability object
        self.reach = Reachability.reachabilityForInternetConnection()
        
        // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
        self.reach!.reachableOnWWAN = false
        
        // Here we set up a NSNotification observer. The Reachability that caused the notification
        // is passed in the object parameter
        NSNotificationCenter.defaultCenter().addObserver(self,
                                                         selector: "reachabilityChanged:",
                                                         name: kReachabilityChangedNotification,
                                                         object: nil)
        
        self.reach!.startNotifier()
        
        return true
}
        
func reachabilityChanged(notification: NSNotification) {
      	if self.reach!.isReachableViaWiFi() || self.reach!.isReachableViaWWAN() {
      		println("Service avalaible!!!")
      	} else {
      		println("No service avalaible!!!")
       	}
}

Tell the world

Head over to Projects using Reachability and add your project for "Maximum Wins!".

reachability's People

Contributors

tonymillion avatar king-wizard avatar sethfri avatar cbowns avatar surfeasyblair avatar alloy avatar fabiopelosin avatar orta avatar reiz avatar soffes avatar ddrscott avatar stevekohls avatar azureatom avatar gzbigegg avatar

Watchers

Logan Hanks avatar Amro Mousa avatar Ryan Cunningham avatar Nick Takayama avatar Fabian Cañas avatar  avatar Stu Hood avatar  avatar David McLaughlin avatar bryan bonczek avatar Bill Couch avatar David Snabel-Caunt avatar Justin McDaniel avatar  avatar Sara Beykpour avatar Ashwin Raghav avatar Israel Ferrer Camacho avatar Jonah Grant avatar Marc Dassonneville avatar Noah Jorgensen avatar  avatar Rémy avatar Reyner Crosby avatar Remy DeCausemaker avatar Patrick Nixon avatar Suyash Sonwalkar avatar Sean F Chan avatar Troy Howard avatar  avatar Peter Grates avatar Pau Tomàs avatar Yi Cheng avatar Jessie avatar Mohammad Almalkawi avatar Kayvon Beykpour avatar  avatar James Cloos avatar Dylan Griffin avatar moomou avatar Bill  avatar Rob Bishop avatar Daniel Wagner-Hall avatar Michael Evans avatar Sorin Cioban avatar  avatar lhbtim avatar David Gil avatar Kris Wilson avatar  avatar Nari Shin avatar Cesar Puerta avatar Michael Braun avatar  avatar Mo avatar Richard Plom avatar  avatar Scott Robinson avatar Frederik Löhner avatar Abe White avatar Mateusz avatar Manuel Deschamps avatar Justin Anderson avatar  avatar Larry Cao avatar Rosy Tucker avatar Rohit Khansili avatar James Alexander avatar Soham Rohankar avatar  avatar Emanuel Mazzilli avatar Pardis Noorzad avatar Raul Hernandez avatar  avatar  avatar David Carr avatar David  avatar Adeel Abbas avatar Steve Kojder avatar Kirk avatar  avatar  avatar Chen-Rui Chou avatar Mia avatar Brandon Au avatar  avatar  avatar  avatar Daniel Montalvo avatar Jules avatar Andrew Sorensen avatar Erik Novales avatar Sneha Patil avatar Alankrit Chona avatar  avatar Benjamin Goh 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.