网络请求优化
2018-07-04 本文已影响31人
考拉小姐_zzzZZZ
网络请求:
1. 在进行网络请求前 先判断网络是否可用
系统方法
#import <SystemConfiguration/SCNetworkReachability.h>
#pragma mark 判断设备能否上网
+ (BOOL)isNet {
struct sockaddr_storage zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.ss_len = sizeof(zeroAddress);
zeroAddress.ss_family = AF_INET;
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags) {
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
return (isReachable&&!needsConnection) ? YES : NO;
}
AF
(参考)
- (void)monitorNetworking
{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case -1:
NSLog(@"未知网络");
break;
case 0:
NSLog(@"网络不可达");
break;
case 1:
{
NSLog(@"GPRS网络");
}
break;
case 2:
{
NSLog(@"wifi网络");
}
break;
default:
break;
}
if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
NSLog(@"有网");
}else{
NSLog(@"没网");
}
}];
}
2. 在进行网络请求前 先判断网络类型, 按需反馈
(参考)
-(NSString *)internetStatus {
Reachability *reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
NSString *net = @"WIFI";
switch (internetStatus) {
case ReachableViaWiFi:
net = @"WIFI";
break;
case ReachableViaWWAN:
net = @"蜂窝数据";
//net = [self getNetType ]; //判断具体类型
break;
case NotReachable:
net = @"当前无网路连接";
default:
break;
}
return net;
}
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
- (NSString *)getNetType
{
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
NSString *currentStatus = info.currentRadioAccessTechnology;
if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyGPRS"]) {
netconnType = @"GPRS";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyEdge"]) {
netconnType = @"2.75G EDGE";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyWCDMA"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSDPA"]){
netconnType = @"3.5G HSDPA";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyHSUPA"]){
netconnType = @"3.5G HSUPA";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMA1x"]){
netconnType = @"2G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORev0"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevA"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyCDMAEVDORevB"]){
netconnType = @"3G";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyeHRPD"]){
netconnType = @"HRPD";
}else if ([currentStatus isEqualToString:@"CTRadioAccessTechnologyLTE"]){
netconnType = @"4G";
}
}