Skip to content

Instantly share code, notes, and snippets.

@eiffelqiu
Created May 27, 2011 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiffelqiu/994400 to your computer and use it in GitHub Desktop.
Save eiffelqiu/994400 to your computer and use it in GitHub Desktop.
Reachability
//
// NPReachability+Backward.h
//
// NPReachability+Backward is a category over the NPReachability class.
//
// It implements some of the Reachability methods and thus makes it easy to integrate with existing projects.
//
#import <Foundation/Foundation.h>
#import "NPReachability.h"
typedef enum {
NotReachable = 0,
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;
#define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"
/**
A backward compatibility category over NPReachability.
An implementation of some of the classic Reachability class.
*/
@interface NPReachability (Backward)
//reachabilityForInternetConnection- checks whether the default route is available.
// Should be used by applications that do not connect to a particular host
+ (NPReachability*) reachabilityForInternetConnection;
//reachabilityForLocalWiFi- checks whether a local wifi connection is available.
+ (NPReachability*) reachabilityForLocalWiFi;
//Start listening for reachability notifications on the current run loop
- (BOOL) startNotifer;
- (void) stopNotifer;
- (APNetworkStatus) currentReachabilityStatus;
@end
//
// NPReachability+Backward.m
//
//
// Created by Tom Susel on 5/10/11.
//
#import "NPReachability+Backward.h"
@interface NPReachability ()
- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags;
@end
void (^reachabilityDidChange)(SCNetworkReachabilityFlags flags) = ^(SCNetworkReachabilityFlags flags){
[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification
object:[NPReachability sharedInstance]];
};
@implementation NPReachability (Backward)
//reachabilityWithHostName- Use to check the reachability of a particular host name.
+ (NPReachability*) reachabilityWithHostName: (NSString*) hostName{
return [NPReachability sharedInstance];
}
//reachabilityWithAddress- Use to check the reachability of a particular IP address.
+ (NPReachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress{
return [NPReachability sharedInstance];
}
//reachabilityForInternetConnection- checks whether the default route is available.
// Should be used by applications that do not connect to a particular host
+ (NPReachability*) reachabilityForInternetConnection{
return [NPReachability sharedInstance];
}
//reachabilityForLocalWiFi- checks whether a local wifi connection is available.
+ (NPReachability*) reachabilityForLocalWiFi{
return [NPReachability sharedInstance];
}
- (BOOL) startNotifer{
[[NPReachability sharedInstance] addHandler:reachabilityDidChange];
return YES;
}
- (void) stopNotifer{
[[NPReachability sharedInstance] removeHandler:reachabilityDidChange];
}
- (NetworkStatus) currentReachabilityStatus{
return [self networkStatusForFlags:
[self currentReachabilityFlags]];
}
#pragma mark -
#pragma mark NPReachability+Backward - Private
- (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags
{
if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
{
// if target host is not reachable
return NotReachable;
}
BOOL retVal = NotReachable;
if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
{
// if target host is reachable and no connection is required
// then we'll assume (for now) that your on Wi-Fi
retVal = ReachableViaWiFi;
}
if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
(flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
{
// ... and the connection is on-demand (or on-traffic) if the
// calling application is using the CFSocketStream or higher APIs
if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
{
// ... and no [user] intervention is needed
retVal = ReachableViaWiFi;
}
}
if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
{
// ... but WWAN connections are OK if the calling application
// is using the CFNetwork (CFSocketStream?) APIs.
retVal = ReachableViaWWAN;
}
return retVal;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment