Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created May 21, 2011 03:59
Show Gist options
  • Save marshluca/984228 to your computer and use it in GitHub Desktop.
Save marshluca/984228 to your computer and use it in GitHub Desktop.
Observer network with Reachablity
- (void)reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"当前没有可用网络"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)observeNetwork
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
Reachability *hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
[hostReach startNotifier];
[hostReach release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment