SoFunction
Updated on 2025-04-12

Detailed explanation of how to add web-free pages in iOS app

Preface

Everyone should encounter such a need. In the process of developing an app, in order to have a better user experience, they often add page displays when there is no network to tell the user that the current network status is not available. So how to achieve real-time monitoring of network status is the issue we will discuss next. Without further ado, let’s take a look at the detailed introduction.

Implementation ideas

My implementation idea is roughly as follows: using the third-party library RealReachability to monitor changes in network state, then add listening to the base class, and then provide external methods in the base class to judge the current network state so that the subclass can make corresponding operations in different network states. Finally, customize a networkless interface. When there is no network connection, add it to the interface. When there is a network, make a data request again and remove the networkless interface.

Specific operations
1. Add a third-party library to the project - RealReachability. I won't introduce it here. Everyone should add it. If you don't know, you can do it.Click here, there will be a more detailed explanation here;

2. Turn on global network monitoring in appdelegate, the specific code is as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 //Open network monitoring [GLobalRealReachability startNotifier];
 return YES;
}

3. Define an enum to represent the three states of network links, and define a variable to judge the current network state, as follows:

typedef NS_ENUM(NSUInteger, XSWNetWorkStatus) {
 
 XSWNetWorkStatusNoInternet,//No network XSWNetWorkStatusFlow,//Train connection XSWNetWorkStatusWifi //wifi link};
//Network Status@property (nonatomic,assign) XSWNetWorkStatus netStatus;

4. Assign values ​​to the netStatus variable through RealReachability in the base class and add monitoring to monitor changes in network status. The specific operations are as follows:

RealReachability *reachability = [RealReachability sharedInstance];
 ReachabilityStatus status = [reachability currentReachabilityStatus];
 switch (status) {
  case 0:
  {
   _netStatus = XSWNetWorkStatusNoInternet;
  }
   break;
  case 1:{
   
   _netStatus = XSWNetWorkStatusFlow;
  }
   break;
  case 2:{
   
   _netStatus = XSWNetWorkStatusWifi;
  }
   break;
  default:
   break;
 }
 // Listen to network status [[NSNotificationCenter defaultCenter] addObserver:self        
selector:@selector(networkChanged:)        
name:kRealReachabilityChangedNotification
object:nil];

5. Implement listening to callback events, assign values ​​to netStatus, and provide external methods to facilitate subclass processing of different network states. The specific operations are as follows:

#pragma mark ==================================================- (void)networkChanged:(NSNotification *)notification
{
  RealReachability *reachability = (RealReachability *);
  ReachabilityStatus status = [reachability currentReachabilityStatus];
  switch (status) {
    case 0:
    {
      _netStatus = XSWNetWorkStatusNoInternet;
      [self monitorNetStateChanged:0];
    }
      break;
    case 1:{
      _netStatus = XSWNetWorkStatusFlow;
      [self monitorNetStateChanged:1];
    }
      break;
    case 2:{
      _netStatus = XSWNetWorkStatusWifi;
      [self monitorNetStateChanged:2];
    }
      break;
    default:
      break;
  }
}
#pragma mark ============================================-(void)monitorNetStateChanged:(NSInteger)netState {
  
  //Subclass implementation}

Subclass rewritemonitorNetStateChanged:(NSInteger)netStateMethod, make corresponding operations based on the current network status, the specific code is as follows:

#pragma mark ==============================================-(void)monitorNetStateChanged:(NSInteger)netState {
  if (netState == 0) {
    //No network displays no network interface, or prompts the user that the current network status is not available  }else{
    //With the Internet, remove the network interface and make the data request again  }
}

At this point, I'll finish it without adding the network interface. If you have a better implementation method or have any questions about the method I've mentioned, please leave a message and I will reply to you as soon as possible. Finally, attach a small demo I wrote.Click here to view, can also be passedLocal download

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.