The corresponding push notification through monitoring network state, let start again request a web page, if you have installed AFNetworking AFNetworkReachabilityManager to monitor network status
Wjnetworkhelper. h typedef enum {StatusUnknown = -1,// Unknown network StatusNotReachable = 0,// No network StatusReachableViaWWAN = 1,// Mobile phone network StatusReachableViaWiFi = 2 //wifi}WJNetworkStatus; /* / typedef void(^WJNetworkStatusBlock)(WJNetworkStatus status); @interface WJNetworkHelper : */ + (void)networkStatusWithBlock:(WJNetworkStatusBlock)networkStatus; */ networkStatusWithBlock:(WJNetworkStatusBlock)networkStatus; @endCopy the code
WJNetworkHelper.m + (void)networkStatusWithBlock:(WJNetworkStatusBlock)networkStatus { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [[AFNetworkReachabilityManager sharedManager]setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
networkStatus ? networkStatus(StatusUnknown) : nil;
break;
case AFNetworkReachabilityStatusNotReachable:
networkStatus ? networkStatus(StatusNotReachable) : nil;
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
networkStatus ? networkStatus(StatusReachableViaWWAN) : nil;
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
networkStatus ? networkStatus(StatusReachableViaWiFi) : nil;
break; }}]; }); }Copy the code
Specific code
# Pragma Mark ————— Network status monitor —————- (void)monitorNetworkStatus{/* */ // Network status changes once, NetworkStatusWithBlock responds once [WJNetworkHelper networkStatusWithBlock:^(WJNetworkStatus networkStatus) {switch (networkStatus) {// Unknown networkcase StatusUnknown:
NSLog(@"Network Environment: Unknown Network"); / / there is no networkcase StatusNotReachable:
NSLog(@"Network environment: No Network");
KPostNotification(KNotificationNetWorkStateChange, @NO);
break; // Mobile networkcase StatusReachableViaWWAN:
NSLog(@"Network environment: Mobile phone comes with network"); // The first time you install the App, you need to request the network once, so save the property isNoNetwork to determine whether you need to request the network. The first time you need to request the network, you need to request the network againif(SettingInfoTools. ShareSettingInfoTools. IsNoNetwork = = YES) {/ / send the request data to inform again [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshHomeData"object:nil]; } KPostNotification(KNotificationNetWorkStateChange, @YES); // Wireless networkcase StatusReachableViaWiFi:
NSLog(@"Network Environment: WiFi");
if (SettingInfoTools.shareSettingInfoTools.isNoNetwork == YES) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshHomeData" object:nil];
}
KPostNotification(KNotificationNetWorkStateChange, @YES);
break; }}]; }Copy the code
// The notification code is placed in the VC that needs to be refreshed. [[NSNotificationCenter defaultCenter] addObserver:self Selector :@ (refreshData) Name :@"refreshHomeData"object:nil]; (void)dealloc {[NSNotificationCenter defaultCenter] removeObserver:self name:@"refreshHomeData" object:nil];
// [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Copy the code
- This code is the code to request the data again
-(void)refreshData {// Change whether the data attribute needs to be called again, save it, Restart App will not appear after this request twice the SettingInfoTools. ShareSettingInfoTools. IsNoNetwork = NO; // Save the attribute [SettingInfoTools saveSettingInfoTools]; // rerequest data [self loadData]; }Copy the code