Skip to content

Instantly share code, notes, and snippets.

@IskenHuang
Created October 3, 2012 01:28
Show Gist options
  • Save IskenHuang/3824393 to your computer and use it in GitHub Desktop.
Save IskenHuang/3824393 to your computer and use it in GitHub Desktop.
iOS-CheckNetWork
//
// CheckNetWork.h
//
//
// Created by Isken Huang on 11/13/10.
// Copyright 2010 Isken Huang. All rights reserved.
//
#import <netinet/in.h>
#import <SystemConfiguration/SCNetworkReachability.h>
@interface CheckNetwork : NSObject {
}
- (BOOL) check;
@end
//
// CheckNetWork.m
//
//
// Created by Isken Huang on 11/13/10.
// Copyright 2010 Isken Huang. All rights reserved.
//
#import "CheckNetwork.h"
@implementation CheckNetwork
- (BOOL) check{
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags){
NSLog(@"Error. Could not recover network reachability flags");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
//NSLog(@"%d || %d", isReachable, needsConnection);
return (isReachable && !needsConnection) ? YES : NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment