• Stars
    star
    258
  • Rank 158,189 (Top 4 %)
  • Language
    Objective-C
  • License
    Other
  • Created over 12 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A better iOS & Mac reachability for a modern age.

KSReachability

By Karl Stenerud

A better reachability for a modern age.

Introduction

A long time ago in an Xcode far away, Apple provided "Reachability", an example Objective-C wrapper to demonstrate the SystemConfiguration Reachability APIs. On the whole it works well enough, but it could be so much more!

KSReachability takes reachability to the next level.

Features

  • Reachability to the network in general, to a host, or to an IPV4 or IPV6 address.
  • Notifications/callbacks via NSNotification, blocks, and KVO.
  • Fetching status values doesn't block.
  • Callbacks and KVO always occur on the main thread, so it's UI-safe.
  • KSReachableOperation: A one-shot operation to perform when reachability is established.
  • Supports iOS and Mac OS X.
  • Can be built with or without ARC, in CLANG or GCC.

Usage

Import:

#import "KSReachability.h"

Create a KSReachability object:

self.reachability = [KSReachability reachabilityToHost:hostname];

Use blocks:

self.reachability.onReachabilityChanged = ^(KSReachability* reachability)
{
    NSLog(@"Reachability changed to %d (blocks)", reachability.reachable);
};

Or use NSNotifications:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onReachabilityChanged:)
                                             name:kDefaultNetworkReachabilityChangedNotification
                                           object:nil];
...

self.reachability.notificationName = kDefaultNetworkReachabilityChangedNotification;
...

- (void) onReachabilityChanged:(NSNotification*) notification
{
    KSReachability* reachability = (KSReachability*)notification.object;
    NSLog(@"Reachability changed to %d (NSNotification)", reachability.reachable);
}

Or use KVO:

[self.reachability addObserver:self
                    forKeyPath:@"reachable"
                       options:NSKeyValueObservingOptionNew
                       context:NULL];
...

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    KSReachability* reachability = (KSReachability*)object;
    NSLog(@"Reachability changed to %d (KVO)", reachability.reachable);
}

Add a reachable operation:

// Create a one-shot operation that gets fired once the host is reachable.
self.reachableOperation = [KSReachableOperation operationWithHost:hostname
                                                        allowWWAN:NO
                                           onReachabilityAchieved:^
                           {
                               [self showAlertWithTitle:@"One-time message"
                                                message:@"Host is reachable!"];
                           }];

Caveats

The Meaning of Reachability

As per Apple's SCNetworkReachability documentation, a remote host is considered reachable when a data packet addressed to that host can leave the local device (i.e. the host is theoretically reachable). It does NOT guarantee that data will actually be received by the host or that the host will respond to a connection request! For example, if the host has a DNS record, but the host itself is down, it will still be considered reachable.

Delays Due to DNS Lookups

KSReachability must do a DNS lookup to determine reachability to a host by name. Since this lookup can take upwards of 10 seconds in extreme cases, it is performed in the background. As a consequence, a newly created KSReachability object will always have its state set to unreachable until this lookup completes. If you need the true reachability to a host, you must wait for the "initialized" property to change to YES (it supports KVO). As an alternative, you can set the callback "onInitializationComplete".

Full Example

I've included a full example project in this repository.

In iOSReachability, see ViewController.h and ViewController.m for details.

In MacReachability, see AppDelegate.h and AppDelegate.m for details.

License

Copyright (c) 2012 Karl Stenerud. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall remain in place in this source code.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

KSCrash

The Ultimate iOS Crash Reporter
Objective-C
4,241
star
2

iOS-Universal-Framework

An XCode project template to build universal frameworks (arm7, arm7s, and simulator) for iOS / iPhone.
Python
2,944
star
3

ObjectAL-for-iPhone

Mac and iOS Audio development, minus the headache. ObjectAL is the easy Objective-C interface to OpenAL, AVAudioPlayer, and audio session management.
Objective-C
885
star
4

Musashi

Motorola 680x0 emulator written in C
C
428
star
5

bo

The Swiss army knife of data examination and manipulation
C
332
star
6

concise-encoding

The secure data format for a modern world
ANTLR
258
star
7

nixos-beginners-handbook

The missing handbook for NixOS beginners
84
star
8

Crash-Manager

An iOS library for recording crash events.
Objective-C
71
star
9

meson-examples

Example C/C++ projects using Meson
Meson
64
star
10

dogma

Dogma: A modernized metalanguage with better expressiveness and binary grammar support
63
star
11

KSJSON

The fastest JSON parser and serializer for Objective-C.
Objective-C
63
star
12

virtual-builders

Builds various virtual environments
Shell
60
star
13

openwrt-relayd-wireless-bridge

Instructions for setting up a wireless bridge in OpenWRT using relayd
50
star
14

Objective-Gems

Useful little gems to use when programming in Objective C
Objective-C
47
star
15

docker-desktops

Desktops inside of Docker images
Shell
42
star
16

Aural

iOS (and later OSX) audio library based on Audio Units (work-in-progress)
C
37
star
17

safe-encoding

Binary-to-text encoding that is safe to pass through modern text processors
C
32
star
18

go-concise-encoding

Golang implementation of Concise Binary and Text Encoding
Go
30
star
19

smalltime

A simple and convenient binary date & time format in 64 bits.
C
26
star
20

bit-tricks

Various tricks for dealing with bits
C
22
star
21

modern-cmake-templates

Templates to get a CMake project started quickly
CMake
20
star
22

DOS-Serial-Library

A serial port / UART library for DOS
C
19
star
23

KSVarArgs

Taking the pain out of variable argument lists in Objective-C.
Objective-C
19
star
24

SynthesizeSingleton

Note: The latest version is at http://github.com/cjhanson/Objective-C-Optimized-Singleton
C
18
star
25

ubuntu-dev-installer

Scripts to install an ubuntu dev environment
Shell
15
star
26

compact-time

Encoding schemes to store a complete time, date, or timestamp in as few bytes as possible for data transmission.
14
star
27

ARCSafe-MemManagement

Makes your code both ARC and non-ARC compatible
Objective-C
13
star
28

go-subvert

Subvert go's runtime system
Go
13
star
29

depixelate

Implementation of various scaling/depixelating techbologies
C
12
star
30

specifications

Specifications for better computing
9
star
31

KSLogging

Basic, lightweight preprocessor-controlled logging for C and Objective-C.
C
9
star
32

ubuntu-server-zfs

Install Ubuntu server with ZFS root
Shell
9
star
33

Universal-Framework-Examples

Examples for more complex usage of universal iOS frameworks
Objective-C
9
star
34

proxmox-containers

Shell
9
star
35

CocosEnhanced

Some useful classes for cocos2d
Objective-C
8
star
36

JSONCompare

A program to compare the speeds of various Objective-C JSON libraries.
Objective-C
8
star
37

vlq

An encoding scheme to compress unsigned integers
C
7
star
38

mac-kvm

Scripts to launch mac os under kvm
Shell
6
star
39

streamux

A minimalist, asynchronous, multiplexing, request-response protocol.
C
5
star
40

go-describe

Rich object descriptions in go
Go
4
star
41

go-gotchas

Common gotchas with golang and how to work around them
4
star
42

lxc-desktop

Script to create complete desktop environments inside LXC containers
Shell
4
star
43

KSRetainTracker

A tool for debugging retain/release issues in Objective-C
Objective-C
4
star
44

enctool

A tool for manipulating data in various encoding formats.
Go
3
star
45

hyver

Hybrid Versioning: Versioning with better U/X
3
star
46

darip

Bulk downloads images from Deviant Art using Mechanicalsoup's simulated browser
Python
3
star
47

Throwaway-IntroMusic

A throwaway repo for demonstrating intro music in ObjectAL
Objective-C
3
star
48

c-smalltime

C implementation of smalltime and nanotime
C
3
star
49

showcase

Showcase of my best work
3
star
50

go-uleb128

Go implementation of unsigned little endian base-128
Go
2
star
51

work-installer

Installer for my work environment
Shell
2
star
52

KSLog

A simple, header-only, async-safe logger for C and C++
C
2
star
53

ubuntu-maintainers-handbook

Ubuntu Maintainer's Handbook
2
star
54

bonjson

A binary serialization format for JSON
2
star
55

go-smalltime

Go implementation of Smalltime and Nanotime
Go
2
star
56

marwaita

Marwaita Theme (Modified)
CSS
2
star
57

compact-float

An encoding scheme to store a floating point value in as few bytes as possible.
2
star
58

open-in-lxc

Start an LXC container wrapping a directory in the host
Shell
2
star
59

fix-crd

Fixes chrome remote desktop
Shell
1
star
60

go-cte

Go implementation of Concise Text Encoding
Go
1
star
61

go-loggedio

Logged I/O for Go
Go
1
star
62

ubuntu-package-fixing

Instructions for fixing an Ubuntu package
1
star
63

navigation

Idiomic navigation through DOM graphs
Java
1
star
64

xor

XORs each octet of the input file with 0xff
C
1
star
65

c-compact-time

C implementation of compact time
C
1
star
66

ubuntu-env-installer

Installer scripts to set up my dev and desktop environments.
Shell
1
star
67

bin

my bin dir
Shell
1
star
68

docker-commafeed

Dockerfile for the Commafeed RSS reader
1
star
69

unicode_range_finder

A quick utility to find and print ranges of unicode codepoints for BNF
Go
1
star
70

lxc-services

Various services running under LXC
Shell
1
star
71

go-equivalence

A go library for comparing objects
Go
1
star
72

c-compact-float

C implementation of compact float
C
1
star
73

endianness

Endianness macros and functions for C and C++
C
1
star
74

kstenerud

Go
1
star
75

varpad

Unlimited padding with an embedded length field
1
star
76

linux-fixes

Fixes for various Linux issues
1
star
77

bash-installer-common

Functions to help install things on an Ubuntu system.
Shell
1
star
78

ksbonjson

A BONJSON codec implemented in C
C
1
star
79

go-compact-time

golang implementation of compact time
Go
1
star
80

KSDelegateForwarding

Multiplex delegates for any object implementing the delegate pattern.
Objective-C
1
star