IOS开发判断当前手机网络

-(BOOL)DoYouHaveAnyNetwork{
NSString *State=@"没有网";
UIApplication *app = [UIApplication sharedApplication];
NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
int netType =0;
for (id child in children){
if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]){
netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];
switch (netType) {
case0:
NSLog(@"........无网络");
break;
case1:
NSLog(@"......2G");
State=@"2G";break;
case2:
NSLog(@".......3G.");
State=@"3G";
break;
case3:
NSLog(@"......4G");
State=@"4G";
break;
case5:
{
NSLog(@".......wifi");
State=@"wifi";}break;default:break;
}
}
}
if ([State isEqualToString:@"没有网"]) {
return NO;
}else{
return YES;
}
}

你可能感兴趣的:(IOS开发判断当前手机网络)