iOS monitors changes in network state in real time
- 2020-06-23 02:00:15
- OfStack
In network applications, real-time monitoring of the network status of user devices is sometimes required, with two purposes:
(1) Let users know their network status and prevent some misunderstandings (such as the incompetence of the application).
(2) Conduct intelligent processing according to users' network status to save user traffic and improve user experience
WIFI network: Automatic download of hd images
4G/3G network: Download thumbnails only
No network: only offline cached data is displayed
There are two commonly used methods:
(1) Reachablity, the library for detecting iOS device network environment provided by Apple View method
(2) Use AFNetworkReachabilityManager in AFN framework to monitor changes in network state
1. Apple officially provides an example program called Reachability for developers to check the state of the network
Please download the sample from Apple sites: before using http: / / xiazai ofstack. com / 201608 / yuanma/Reachability (ofstack. com). rar
Then add Reachability.h and ES37en.m to your project and reference SystemConfiguration.framework and you are ready to use.
Three network states are defined in Reachability:
typedef enum : NSInteger {
NotReachable = 0, // There is no connection
ReachableViaWiFi, // use 3G/GPRS network
ReachableViaWWAN // use WiFi network
} NetworkStatus;
We can enable real-time monitoring after the program starts
// AppDelegate.m
@interface AppDelegate ()
@property (nonatomic, strong) Reachability *reachability;
@end
// Program starter, start network monitoring
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Set up sites for network detection
NSString *remoteHostName = @"www.apple.com";
self.reachability = [Reachability reachabilityWithHostName:remoteHostName];
// Sets the notification function for network state changes
[[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(@" No Internet! ");
break;
}
case ReachableViaWWAN: {
NSLog(@"4G/3G");
break;
}
case ReachableViaWiFi: {
NSLog(@"WiFi");
break;
}
}
}
}
- (void)dealloc
{
[_reachability stopNotifier];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
2. Use AFNetworkReachabilityManager in AFN framework to listen for changes in network state
// use AFN Framework to detect changes in network state
-(void)AFNReachability
{
//1. Create a network listener manager
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
//2. Listen for changes in network state
/*
AFNetworkReachabilityStatusUnknown = The unknown
AFNetworkReachabilityStatusNotReachable = There is no Internet
AFNetworkReachabilityStatusReachableViaWWAN = 3G
AFNetworkReachabilityStatusReachableViaWiFi = WIFI
*/
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@" The unknown ");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@" There is no Internet ");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WIFI");
break;
default:
break;
}
}];
//3. To start listening to
[manager startMonitoring];
}