Customize the error page

更新时间:
复制 MD 格式

If a miniapp fails to load because of a network issue or an inaccessible website, an error message appears, such as "Network connection failed (-1009)".

This topic describes how to customize the error page.

Procedure

You can customize the error page in two steps:

  1. In the H5 base class, add a listener for the kEvent_Navigation_Error method. Implement the - (void)handleEvent:(PSDEvent *)event method in your MPH5WebViewController () <PSDPluginProtocol> class extension:

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

    The 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. In the - (void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions method of DTFrameworkInterface, you can set the error page and the H5 base class. The errorHtmlPath parameter specifies the path to the HTML error page that is displayed when an H5 page fails to load. By default, the system reads the MPNebulaAdapter.bundle/error.html file.

    d6c2a611005e16d80540214e1bde4e28

    The code for 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>