Two methods for iOS to detect network state

  • 2021-01-02 22:00:05
  • OfStack

Generally, there are two ways, both are the third square frame, the wheel, as long as it works, use it first, then optimize it later.

1: Reachability

1. First add the header file "Reachability.h" to AppDelegate.h to import the frame SystemConfiguration.frame.

2. AppDelegate. m is implemented as follows:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// Turn on network status monitoring 
// To subscribe to real-time notifications of network state changes. The import Reachability.h Header file, and then register 1 Object to subscribe to the information of network state change. The information name of network state change is kReachabilityChanged-Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
// Determine whether the current network is available by checking whether a host is accessible: 
self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"] ;
// Start listening, it will start 1 a run loop
[self.hostReach startNotifier];
}
-(void)reachabilityChanged:(NSNotification *)note{
Reachability *currReach = [note object];
NSParameterAssert([currReach isKindOfClass:[Reachability class]]);
// Respond to connection changes with processing actions 
NetworkStatus status = [currReach currentReachabilityStatus];
// If you are not connected to the network, a live alert will pop up 
self.isReachable = YES;
if(status == NotReachable){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Network connection anomaly " message:nil delegate:nil cancelButtonTitle:@" determine " otherButtonTitles:nil];
[alert show];
[alert release];
self.isReachable = NO;
return;
}
if (status==kReachableViaWiFi||status==kReachableViaWWAN) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Network connection information " message:@" Network connection normal " delegate:nil cancelButtonTitle:@" determine " otherButtonTitles:nil];
// [alert show];
[alert release];
self.isReachable = YES;
}
}

Then add viewWillAppear: to each page:


-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDlg.isReachable){
NSLog(@" Network connected ");// Execute the normal network code 
}
else{
NSLog(@" Network connection anomaly ");// Code that executes a network exception 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Network connection anomaly " message:nil delegate:nil cancelButtonTitle:@" determine " otherButtonTitles:nil];
[alert show];
[alert release];
}
}

This allows you to detect sudden network outages and connections while the program is running. The Reachability class is actually Apple's encapsulation of SCNetworkReachability API, which is defined in the SystemConfigure.framework library. You can also use the native SCNetworkReachability class directly if you have other special needs.

2: AFNetworking monitoring

1. Import frame, and header file #import < AFNetworkReachabilityManager.h >

2. Code:


-(void)afn{
//1. Create a network status monitoring manager 
AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
// Turn it on. Remember to turn it on, or it won't go block
[manger startMonitoring];
//2. Listen to change 
[manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
/*
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 2,
*/
switch (status) {
case AFNetworkReachabilityStatusUnknown:
NSLog(@" The unknown ");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@" There is no Internet ");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G|4G");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"WiFi");
break;
default:
break;
}
}];
}

Related articles: