Customize error page for iOS Mini Program

更新时间:
复制 MD 格式

When loading the mini program, if it fails to load the page or the website cannot be opened, an error similar to the following will appear.

"Network can't connect (-1009)"

This topic introduces how to customize the error in the above figure.

Procedure

Customizing the error page falls into the following 2 steps:

  1. Listen to the kEvent_Navigation_Error method in HTML5 base class.

    Introduce - (void)handleEvent:(PSDEvent *)event method via MPH5WebViewController () <PSDPluginProtocol> interface:

     - (void)handleEvent:(PSDEvent *)event
     {
         [super handleEvent:event];
    
             if ([kEvent_Navigation_Error isEqualToString:event.eventType]) {
             [self handleContentViewDidFailLoad:(id)event];
         }
     }

    handleContentViewDidFailLoad method is as follows:

     - (void)handleContentViewDidFailLoad:(PSDNavigationEvent *)event
     {
         PSDNavigationEvent *naviEvent = (PSDNavigationEvent *)event;
         NSError *error = naviEvent.error;
         [MPH5ErrorHelper handlErrorWithWebView:(WKWebView *)self.psdContentView error:error];
     }
  2. Set error page and HTML5 base class in afterDidFinishLaunchingWithOptions method.

    In which, errorHtmlPath is the HTML error page path displayed when it fails to load the HTML5 page, and reads MPNebulaAdapter.bundle/error.html by default.

    The code of myerror is as follows:

     <!DOCTYPE html>
     <html lang="en">
     <head>
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <title>Document</title>
     </head>
     <body>
         Custom error message
     </body>
     </html>