Created
October 3, 2012 01:28
-
-
Save IskenHuang/3824393 to your computer and use it in GitHub Desktop.
iOS-CheckNetWork
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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