Latest2.0.6
Homepagehttps://github.com/belkevich/reachability-ios
LicenseMIT
Platformsios 5.0, osx 10.7, requires ARC
FrameworksSystemConfiguration, Foundation
Authors

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];

History

Releases

Updates

Follow updates on twitter @okolodev

Latest podspec

{
    "name": "SCNetworkReachability",
    "version": "2.0.6",
    "summary": "Flexible network reachability with blocks for iOS and OS X.",
    "homepage": "https://github.com/belkevich/reachability-ios",
    "social_media_url": "https://twitter.com/okolodev",
    "license": {
        "type": "MIT",
        "file": "LICENSE.txt"
    },
    "authors": {
        "Alexey Belkevich": "[email protected]"
    },
    "source": {
        "git": "https://github.com/belkevich/reachability-ios.git",
        "tag": "2.0.6"
    },
    "frameworks": [
        "SystemConfiguration",
        "Foundation"
    ],
    "requires_arc": true,
    "platforms": {
        "ios": "5.0",
        "osx": "10.7"
    },
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Classes/P*/*.{h,m}",
            "dependencies": {
                "macros_blocks": [
                    "~> 0.0.1"
                ]
            }
        },
        {
            "name": "Multithreading",
            "dependencies": {
                "SCNetworkReachability/Core": []
            },
            "source_files": "Classes/Subspecs/Multithreading/*.{h,m}"
        },
        {
            "name": "Shared",
            "dependencies": {
                "SCNetworkReachability/Core": [],
                "ABMultiton": [
                    "~> 2.0"
                ]
            },
            "source_files": "Classes/Subspecs/Shared/*.{h,m}"
        },
        {
            "name": "Compatibility",
            "dependencies": {
                "SCNetworkReachability/Core": []
            },
            "source_files": "Classes/Subspecs/Compatibility/*.{h,m}"
        },
        {
            "name": "All",
            "dependencies": {
                "SCNetworkReachability/Core": [],
                "SCNetworkReachability/Shared": [],
                "SCNetworkReachability/Multithreading": [],
                "SCNetworkReachability/Compatibility": []
            }
        }
    ]
}

Pin It on Pinterest

Share This