Latest3.2
Homepagehttps://github.com/tonymillion/Reachability
LicenseBSD Copyright (c) 2011, Tony Million.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Platformsios 6.0, osx 10.8, requires ARC
FrameworksSystemConfiguration
Authors

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.

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!".

Latest podspec

{
    "name": "Reachability",
    "version": "3.2",
    "summary": "ARC and GCD Compatible Reachability Class for iOS and OS X. Drop in replacement for Apple Reachability.",
    "homepage": "https://github.com/tonymillion/Reachability",
    "authors": {
        "Tony Million": "[email protected]"
    },
    "social_media_url": "http://twitter.com/tonymillion",
    "license": {
        "type": "BSD",
        "text": "Copyright (c) 2011, Tony Million.nAll rights reserved.nnRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:nn1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.nn2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.nnTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.n"
    },
    "source": {
        "git": "https://github.com/tonymillion/Reachability.git",
        "tag": "v3.2"
    },
    "source_files": "Reachability.{h,m}",
    "frameworks": "SystemConfiguration",
    "requires_arc": true,
    "platforms": {
        "ios": "6.0",
        "osx": "10.8"
    }
}

Pin It on Pinterest

Share This