In network applications, it is sometimes necessary to monitor the network status of user equipment in real time, which has two purposes:
(1) Let users understand their network status and prevent some misunderstandings (such as incompetence in the application)
(2) Intelligent processing is carried out based on the user's network status, saving user traffic and improving user experience
WIFI network: Automatically download high-definition pictures
4G/3G network: only download thumbnails
No network: Only offline cached data is displayed
There are two common methods:
(1) Use the library provided by Apple View to detect iOS device network environment Reachable
(2) Use the AFNetworkReachabilityManager in the AFN framework to listen for changes in network status.
1. Apple officially provides a sample program called Reachability to facilitate developers to detect network status.
Before using, please download the example from the Apple website:http://xiazai./201608/yuanma/Reachability().rar
Then add and to your project and reference it, and it is ready to use it.
Reachability defines three network states:
typedef enum : NSInteger { NotReachable = 0, //No connection ReachableViaWiFi, //Use 3G/GPRS network ReachableViaWWAN //Use WiFi network } NetworkStatus;
We can start real-time monitoring after the program is started.
// @interface AppDelegate () @property (nonatomic, strong) Reachability *reachability; @end // Program starter, start network monitoring- (void)applicationDidFinishLaunching:(UIApplication *)application { // Set up a network detection site NSString *remoteHostName = @""; = [Reachability reachabilityWithHostName:remoteHostName]; // Set notification function when 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 the AFNetworkReachabilityManager in the AFN framework to listen for changes in network state
//Use the AFN framework to detect changes in network state-(void)AFNReachability { //1. Create a network monitor manager AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager]; //2. Listen to the changes in network status /* AFNetworkReachabilityStatusUnknown = Unknown AFNetworkReachabilityStatusNotReachable = No network AFNetworkReachabilityStatusReachableViaWWAN = 3G AFNetworkReachabilityStatusReachableViaWiFi = WIFI */ [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusUnknown: NSLog(@"unknown"); break; case AFNetworkReachabilityStatusNotReachable: NSLog(@"No Internet"); break; case AFNetworkReachabilityStatusReachableViaWWAN: NSLog(@"3G"); break; case AFNetworkReachabilityStatusReachableViaWiFi: NSLog(@"WIFI"); break; default: break; } }]; //3. Start monitoring [manager startMonitoring]; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.