In the network application, sometimes need to monitor the network status of the user device in real time, there are two purposes:
(1) Let users know their network status, to prevent some misunderstandings (such as the application of incompetence)
(2) According to the user's network status intelligent processing, save user traffic, improve the user experience
WiFi network: Download HD images automatically
4G/3G network: Download thumbnails only
No network: Show only cached data that is offline
commonly used in the following two ways:
(1), using the Apple view method to detect the iOS device network environment for the library reachablity
(2), use the Afnetworkreachabilitymanager in AFN frame to monitor the change of network state
Apple officially provides a sample program called Reachability, which allows developers to detect network status
Download the sample from the Apple website before using: http://xiazai.jb51.net/201608/yuanma/Reachability (jb51.net). rar
Then add Reachability.h and REACHABILITY.M to your project, and reference Systemconfiguration.framework, you can use it.
3 network states are defined in reachability:
?
1234567 |
typedef enum : NSInteger { NotReachable = 0, //无连接 ReachableViaWiFi, //使用3G/GPRS网络 ReachableViaWWAN //使用WiFi网络 } NetworkStatus; |
We can start real-time monitoring after the program starts
?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
// AppDelegate.m
@interface AppDelegate ()
@property (nonatomic, strong) Reachability *reachability;
@end
// 程序启动器,启动网络监视
- (
void
)applicationDidFinishLaunching:(UIApplication *)application {
// 设置网络检测的站点
NSString *remoteHostName = @
"www.apple.com"
;
self.reachability = [Reachability reachabilityWithHostName:remoteHostName];
// 设置网络状态变化时的通知函数
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@
"kNetworkReachabilityChangedNotification" object:nil];
[self updateStatus];
}
- (
void
)reachabilityStatusChange:(NSNotification *)notification
{
Reachability* curReach = [notification object];
NSParameterAssert([curReach isKindOfClass:[Reachability
class
]]);
[self updateInterfaceWithReachability:curReach];
} - (
void
)updateInterfaceWithReachability:(Reachability *)reachability
{
if (reachability == _reachability)
{
NetworkStatus netStatus = [reachability currentReachabilityStatus];
switch (netStatus)
{
case NotReachable: {
NSLog(@
"没有网络!"
);
break
;
}
case ReachableViaWWAN: {
NSLog(@
"4G/3G"
);
break
;
}
case ReachableViaWiFi: {
NSLog(@
"WiFi"
);
break
;
}
}
}
}
- (
void
)dealloc
{
[_reachability stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
|
Second, use the Afnetworkreachabilitymanager in AFN frame to monitor the change of network state
?
123456789101112131415161718192021222324252627282930313233343536 |
//使用AFN框架来检测网络状态的改变
-(
void
)AFNReachability
{
//1.创建网络监听管理者
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
//2.监听网络状态的改变
/*
AFNetworkReachabilityStatusUnknown = 未知
AFNetworkReachabilityStatusNotReachable = 没有网络
AFNetworkReachabilityStatusReachableViaWWAN = 3G
AFNetworkReachabilityStatusReachableViaWiFi = WIFI
*/
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@
"未知"
);
break
;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@
"没有网络"
);
break
;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@
"3G"
);
break
;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@
"WIFI"
);
break
;
default
:
break
;
}
}];
//3.开始监听
[manager startMonitoring];
}
|
ios--real-time monitoring of network status changes