首先要导入必要的库:
#import <ifaddrs.h>
#import <net/if.h>
#import <SystemConfiguration/CaptiveNetwork.h>
逻辑代码:
- (BOOL) isWiFiEnabled {
NSCountedSet * cset = [NSCountedSet new];
struct ifaddrs *interfaces;
if( ! getifaddrs(&interfaces) ) {
for( struct ifaddrs *interface = interfaces; interface; interface = interface->ifa_next) {
if ( (interface->ifa_flags & IFF_UP) == IFF_UP ) {
[cset addObject:[NSString stringWithUTF8String:interface->ifa_name]];
}
}
}
return [cset countForObject:@"awdl0"] > 1 ? YES : NO;
}
在想要判断的地方调用
if(![self isWiFiEnabled]){
//弹框提示没有联网
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"提示" message:@"当前没有wifi网络连接!"];
[alert setDestructiveButtonWithTitle:@"确定" block:^{ }];
[alert show];
}
#import <ifaddrs.h>
#import <net/if.h>
#import <SystemConfiguration/CaptiveNetwork.h>
逻辑代码:
- (BOOL) isWiFiEnabled {
NSCountedSet * cset = [NSCountedSet new];
struct ifaddrs *interfaces;
if( ! getifaddrs(&interfaces) ) {
for( struct ifaddrs *interface = interfaces; interface; interface = interface->ifa_next) {
if ( (interface->ifa_flags & IFF_UP) == IFF_UP ) {
[cset addObject:[NSString stringWithUTF8String:interface->ifa_name]];
}
}
}
return [cset countForObject:@"awdl0"] > 1 ? YES : NO;
}
在想要判断的地方调用
if(![self isWiFiEnabled]){
//弹框提示没有联网
BlockAlertView *alert = [BlockAlertView alertWithTitle:@"提示" message:@"当前没有wifi网络连接!"];
[alert setDestructiveButtonWithTitle:@"确定" block:^{ }];
[alert show];
}
呵呵,不错。
不过直接使用Apple提供的 Reachability文件,使用更方便,也不容易出错,代码质量有保证