项目中添加网络判断

前提:工程添加:SystemConfiguration.framework framework

然后在需要判断的类中包含头文件:

#import "Reachability.h"

【如果你使用的ASIHTTPRequest类库,那么直接import Reachbility.h就可以了,ASIHTTP类库里包含Reachbility.h和.m】

//afnetworking  网络判断

-(BOOL) isConnectionAvailable{

UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:nilmessage:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:nil, nil];

BOOL isExistenceNetwork = YES;

Reachability *reach = [ReachabilityreachabilityWithHostName:@"www.apple.com"];

switch ([reach currentReachabilityStatus]) {

caseNotReachable:

isExistenceNetwork = NO;

alertView.title = @ "当前网络连接不可用";

[alertView show];

//            NSLog(@"notReachable");

break;

caseReachableViaWiFi:

isExistenceNetwork = YES;

alertView.title = @ "您正在使用Wi-Fi";

[alertView show];

//NSLog(@"WIFI");

break;

caseReachableViaWWAN:

isExistenceNetwork = YES;

alertView.title = @ "您正在使用手机流量";

[alertView show];

//NSLog(@"3G");

break;

}

return isExistenceNetwork;​

}

你可能感兴趣的:(项目中添加网络判断)