估计大家做iOS 开发时用的最多的网络监测就是Reachability,以及各种在此基础上封装的一些库
你看到的上面这张图就是Reachable,用到最多的就是isReachable,isReachableViaWWAN,isReachableViaWiFi分别表示网络是否可用,2g、3g、4g是否可用,wifi是否能用。
但是这个监测网络的方法都忽略了一种情况,有WiFi连接但是没有数据传输的情况。
在这种情况下上面能监测到WIFi连接,而没有数据进行传输,所以就可能给你造成视觉上的影响并且你在这时候弹出提示有网络连接,但是并没有数据传输,是不是有种被网络欺骗的感觉所以最好的解决方案是改造这个Reachability,我的想法是通过SCNetworkReachabilityFlags这个入手,当连接wifi的时候,这个flags的值是1<<2.只是有这个想法,还没有实施。。。。
当然也有个取巧的方法,你每次用这个方法的的把它放在网络请求里面
- (void) httpRequest:(NSString *)url
withData:(NSDictionary *)postData
requestMethod:(NSString*)httpMethod
failedBlock:(GBHEFailedBlock)failedBlock
completionBlock:(GBHECompletionBlock)completionBlock
{
Reachability *_reach = [Reachability reachabilityForInternetConnection];
if (_reach.isReachable) {
// [[ToastView getInstance]displayToastWithMessage:@"哈哈"];
}
else{
// [[ToastView getInstance]displayToastWithMessage:@"请求超时"];
}
//1..检查网络连接(苹果公司提供的检查网络的第三方库 Reachability)
//AFN 在 Reachability基础上做了一个自己的网络检查的库, 基本上一样
if (isFirst == NO) {
//网络只有在startMonitoring完成后才可以使用检查网络状态
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
canCHeckNetwork = YES;
}];
isFirst = YES;
}
//只能在监听完善之后才可以调用
BOOL isOK = [[AFNetworkReachabilityManager sharedManager] isReachable];
// BOOL is3GOK = [[AFNetworkReachabilityManager sharedManager]isReachableViaWWAN];
//网络有问题
if(isOK == NO && canCHeckNetwork == YES){
// NSError *error = [NSError errorWithDomain:@"网络错位" code:100 userInfo:nil];
return;
MyLog(@"网络!!");
}
url = [NSString stringWithFormat:@"%s%@",PORTOCOL_APP_ADDR,url];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
manager.requestSerializer.timeoutInterval=10;
NSComparisonResult comparison1 = [httpMethod caseInsensitiveCompare:@"GET"];
NSComparisonResult comparisonResult2 = [httpMethod caseInsensitiveCompare:@"POST"];
if (comparison1 == NSOrderedSame)
{
[manager GET:url parameters:postData success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (completionBlock) {
completionBlock(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failedBlock) {
[[ToastView getInstance]displayToastWithMessage:@"请求超时"];
failedBlock(error);
MyLog(@"%@",error);
}
}];
}
if (comparisonResult2 == NSOrderedSame)
{
BOOL isFile = NO;
for (NSString *key in postData.allKeys) {
id value = postData[key];
if ([value isKindOfClass:[NSData class]]) {
isFile = YES;
break;
}
}
if (!isFile) {//判断是上传数据还是下请求数据
[manager POST:url parameters:postData success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (completionBlock) {
completionBlock(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failedBlock) {
[[ToastView getInstance]displayToastWithMessage:@"请求超时"];
failedBlock(error);
}
}];
}else
{
[manager POST:url
parameters:postData
constructingBodyWithBlock:^(id formData) {
//取出需要上传的图片数据
for (NSString *key in postData) {
id value = postData[key];
if ([value isKindOfClass:[NSData class]]) {
[formData appendPartWithFileData:value
name:key
fileName:key
mimeType:@"image/jpeg"];
}
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (completionBlock) {
completionBlock(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failedBlock) {
[[ToastView getInstance]displayToastWithMessage:@"请求超时"];
failedBlock(error);
}
}];
}
}
}