Skip to content

Instantly share code, notes, and snippets.

@leebyron
Created June 14, 2011 23:20
Show Gist options
  • Save leebyron/1026174 to your computer and use it in GitHub Desktop.
Save leebyron/1026174 to your computer and use it in GitHub Desktop.
innernets
- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
// check for future network connectivity changes
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateNetStatus:)
name:kNetConnectionChangeNotification
object:[NetConnection netConnection]];
}
- (void)updateNetStatus:(NSNotification *)notif
{
if ([[NetConnection netConnection] isOnline]) {
[[NSSound soundNamed:@"Ping"] play];
} else {
NSBeep();
}
}
/**
* NetConnection is a small class responsible for keeping track of when your
* computer has access to the internet. It does so via the isOnline method
* and posting notifications to the NSNotificiationCenter.
*
* @author leebyron lee@leebyron.com
*
*
* To determine at any time if you are online
*
* BOOL live = [[NetConnection netConnection] isOnline];
*
*
* To keep track of net connection change events, subscribe to the event via
* the NSNotificationCenter:
*
* NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
* [nc addObserver:self
* selector:@selector(netConnectionChanged:)
* name:kNetConnectionChangeNotification
* object:[NetConnection netConnection]];
*
* - (void)netConnectionChanged:(NSNotification*)notif {
* BOOL live = [[NetConnection netConnection] isOnline];
* }
*
*/
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <ApplicationServices/ApplicationServices.h>
#define kNetConnectionChangeNotification @"NetConnectionChangeNotification"
@interface NetConnection : NSObject {
SCDynamicStoreRef dynamicStore;
CFRunLoopSourceRef runLoopSource;
BOOL isOnline;
NSURL* connectivityURL;
}
+ (NetConnection*)netConnection;
- (BOOL)isOnline;
@end
#import "NetConnection.h"
//---------------------------------------------------------------------------
@interface NetConnection (Private)
- (id)initWithURL:(NSURL*)url;
- (BOOL)isNetworkConnected;
- (void)changedKey:(NSString*)key userInfo:(NSDictionary*)userInfo;
@end
//---------------------------------------------------------------------------
void _NetConnectionNotificationCallback(
SCDynamicStoreRef store,
CFArrayRef changedKeys,
void* source) {
NSString* key;
NSDictionary* info;
NSEnumerator* keysE = [(NSArray *)changedKeys objectEnumerator];
while (key = [keysE nextObject]) {
info = (NSDictionary*)SCDynamicStoreCopyValue(store, (CFStringRef)key);
[(NetConnection*)source changedKey:key userInfo:info];
[info release];
}
}
//---------------------------------------------------------------------------
@implementation NetConnection
static NetConnection* instance = nil;
+ (NetConnection*)netConnection
{
if (instance == nil) {
NSURL* url = [NSURL URLWithString:@"http://www.google.com/"];
instance = [[NetConnection alloc] initWithURL:url];
}
return instance;
}
- (void)dealloc
{
// remove dynamic store
CFRunLoopRemoveSource(
[[NSRunLoop currentRunLoop] getCFRunLoop],
runLoopSource,
kCFRunLoopCommonModes
);
CFRelease(runLoopSource);
CFRelease(dynamicStore);
[super dealloc];
}
- (BOOL)isOnline
{
return isOnline;
}
@end
//---------------------------------------------------------------------------
@implementation NetConnection (Private)
- (id)initWithURL:(NSURL*)url
{
self = [super init];
if (self) {
// check present connectivity
connectivityURL = [url retain];
isOnline = [self isNetworkConnected];
// set up dynamic store
SCDynamicStoreContext context = {0, (void *)self, NULL, NULL, NULL};
dynamicStore = SCDynamicStoreCreate(
NULL,
(CFStringRef) [[NSBundle mainBundle] bundleIdentifier],
_NetConnectionNotificationCallback,
&context
);
// add dynamic store to run loop
runLoopSource = SCDynamicStoreCreateRunLoopSource(NULL, dynamicStore, 0);
CFRunLoopAddSource(
[[NSRunLoop currentRunLoop] getCFRunLoop],
runLoopSource,
kCFRunLoopCommonModes
);
// only observe ip changes
BOOL success = SCDynamicStoreSetNotificationKeys(
dynamicStore,
NULL,
(CFArrayRef)[NSArray arrayWithObject:@"State:/Network/Global/IP.*"]
);
if (!success) {
NSLog(@"Error: keys could not be observed.");
}
}
return self;
}
- (BOOL)isNetworkConnected
{
SCNetworkConnectionFlags status;
BOOL success = SCNetworkCheckReachabilityByName(
[[connectivityURL host] UTF8String],
&status
);
success = success
&& (status & kSCNetworkFlagsReachable)
&& !(status & kSCNetworkFlagsConnectionRequired);
return success;
}
- (void)changedKey:(NSString*)key userInfo:(NSDictionary*)userInfo
{
BOOL wasOnline = isOnline;
isOnline = userInfo != nil;
if (wasOnline != isOnline) {
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:kNetConnectionChangeNotification
object:self
userInfo:userInfo];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment