方法一:
1.在AppDelegate的声明文件中增加网络状态属性:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, assign, readonly) ReachabilityStatus networkStatus;
@end
2.在AppDelegate的实现文件中使用RAC监听绑定网络状态:
- (void)configNetworkStatus {
[GLobalRealReachability startNotifier];
RAC(self,networkStatus) = [[[[[NSNotificationCenter defaultCenter] rac_addObserverForName: kRealReachabilityChangedNotification object: nil] map:^id(NSNotification *notif) {
return @([notif.object currentReachabilityStatus]);
}] startWith: @([GLobalRealReachability currentReachabilityStatus])] distinctUntilChanged];
}
方法二:
+ (BOOL)isConnectedToNetwork
{
// 创建零地址,0.0.0.0的地址表示查询本机的网络连接状态
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
/**
* SCNetworkReachabilityRef: 用来保存创建测试连接返回的引用
*
* SCNetworkReachabilityCreateWithAddress: 根据传入的地址测试连接.
* 第一个参数可以为NULL或kCFAllocatorDefault
* 第二个参数为需要测试连接的IP地址,当为0.0.0.0时则可以查询本机的网络连接状态.
* 同时返回一个引用必须在用完后释放.
* PS: SCNetworkReachabilityCreateWithName: 这个是根据传入的网址测试连接,
* 第二个参数比如为"www.apple.com",其他和上一个一样.
*
* SCNetworkReachabilityGetFlags: 这个函数用来获得测试连接的状态,
* 第一个参数为之前建立的测试连接的引用,
* 第二个参数用来保存获得的状态,
* 如果能获得状态则返回TRUE,否则返回FALSE
*
*/
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
printf("Error. Could not recover network reachability flagsn");
return NO;
}
/**
* kSCNetworkReachabilityFlagsReachable: 能够连接网络
* kSCNetworkReachabilityFlagsConnectionRequired: 能够连接网络,但是首先得建立连接过程
* kSCNetworkReachabilityFlagsIsWWAN: 判断是否通过蜂窝网覆盖的连接,
* 比如EDGE,GPRS或者目前的3G.主要是区别通过WiFi的连接.
*
*/
BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
return (isReachable && !needsConnection) ? YES : NO;
}