Cross-platform WebView packaging device and method based on conditional compiling

The cross-platform WebView wrapper with conditional compilation solves the problems of inconsistent APIs and error feedback in cross-platform WebView implementations, achieving a unified interface and consistent interaction for cross-platform WebView, and improving code reusability and user experience.

CN120994177AActive Publication Date: 2025-11-21SHANGHAI GREAT WISDOM SHENJIU INFORMATION TECH CO LTD

Patent Information

Application Number
CN202511508871.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-22
Publication Date
2025-11-21
Estimated Expiration
2045-10-22

AI Technical Summary

Technical Problem

Cross-platform WebView implementations suffer from inconsistent APIs, significant behavioral differences, and inconsistent error feedback. Existing technologies such as CN114546352A and CN119783118B have failed to effectively solve the compatibility and reusability issues between multiple platforms.

Method used

A cross-platform WebView wrapper based on conditional compilation is adopted, including a WebView wrapper layer, a platform adaptation layer and an underlying platform layer. The platform type is detected by Qt preprocessor macros, the corresponding browser engine implementation class is dynamically selected and instantiated, and a unified API interface and standardized error handling module are provided to shield platform differences.

Benefits of technology

It implements a unified interface call for WebView across platforms, improves code reusability, and ensures consistent interaction and user-friendly error feedback across different platforms.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120994177A_ABST
    Figure CN120994177A_ABST
Patent Text Reader

Abstract

The invention provides a cross-platform WebView packaging device and method based on conditional compiling. The cross-platform WebView packaging device comprises a WebView packaging layer, a platform adaptation layer and a bottom platform layer, the WebView encapsulation layer provides a unified API interface as an entry directly called by a developer, and shields the difference of a bottom platform; the platform adaptation layer detects the type of a target operating system in a compiling stage by utilizing a Qt preprocessor macro, and dynamically selects and instantiates a bottom browser view most matched with the corresponding platform according to a detection result; and the bottom platform layer comprises a browser engine implementation class exclusive to each platform. According to the method, a unified packaging layer and an API interface are established, platform differences are digested at a bottom layer, and upper-layer service codes can be written once and run everywhere.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of communication technology, and more specifically, to a cross-platform WebView encapsulation device and method based on conditional compilation. Background Technology

[0002] With the rise of various PC operating systems, especially the development of domestic IT innovation systems and HarmonyOS, cross-platform development has become an inevitable trend. Among numerous cross-platform solutions, the Qt framework, with its powerful C++ library, rich components, and excellent support for desktop and embedded systems, has become a favored tool for developers. Meanwhile, displaying web page content within applications has also become a basic function. WebView, as the core component carrying web content, exhibits significant differences in its implementation mechanisms across different platforms, posing a serious challenge to cross-platform development. To address this issue, this invention proposes a cross-platform WebView encapsulation device and method based on conditional compilation. This method masks platform differences through unified interface calls, improving code reusability. It dynamically enables the corresponding platform's WebView implementation module based on the platform identifier in the compilation environment, thereby achieving adaptation on multiple operating systems such as Windows, macOS, and HarmonyOS.

[0003] Patent document CN119783118B discloses a method and system for cross-platform anomaly detection and repair based on WebView. It initializes a WebView component, loads an H5 page, and injects multiple listeners to detect anomalies in the H5 page's operation. When any of these listeners triggers an anomaly, the system immediately refreshes the H5 page and automatically skips the abnormal function or component. Simultaneously, the anomaly data is uploaded to a server, where it is categorized and analyzed to identify the anomaly and automatically generate a dynamic repair patch, which is then injected into the H5 page. This enables rapid repair of anomalies and allows for accurate detection and location of cross-platform anomalies, as well as intelligent filtering and page restoration. However, patent document CN119783118B focuses on anomaly detection and repair in cross-platform applications rather than fundamentally addressing the issue of inconsistent WebView APIs across platforms.

[0004] Patent document CN114546352A discloses a communication API framework and method based on Android WebView, including: WebView, JSAPI, and Java API wrapper class; the WebView communicates with the JSAPI, and the JSAPI communicates with the Java API wrapper class; the WebView directly calls the Java API wrapper class through the JSAPI, so that the Java API wrapper class establishes communication with the terminal device to obtain JSAPI data.

[0005] However, patent document CN114546352A has platform limitations; its entire technical architecture is deeply tied to the Android system, lacking portability and scalability, and failing to form a unified development paradigm. This invention addresses the compatibility and reuse issues across multiple platforms. Summary of the Invention

[0006] In view of the deficiencies in the prior art, the purpose of this invention is to provide a cross-platform WebView encapsulation device and method based on conditional compilation.

[0007] According to the present invention, a cross-platform WebView encapsulation device based on conditional compilation includes: a WebView encapsulation layer, a platform adaptation layer, and a bottom platform layer; The WebView encapsulation layer provides a unified API interface as an entry point for developers to directly call, shielding the differences between underlying platforms; The platform adaptation layer uses Qt preprocessor macros to detect the target operating system type during the compilation stage, and dynamically selects and instantiates the underlying browser view that best matches the platform based on the detection results. The underlying platform layer includes browser engine implementation classes specific to each platform.

[0008] Preferably, the platform includes: Windows / Linux platform, macOS platform, and OpenHarmony platform.

[0009] Preferably, the preprocessor macros include Q_OS_WIN or Q_OS_LINUX, Q_OS_DARWIN, and Q_OS_OPENHARMONY, which correspond to the Windows / Linux platform, macOS platform, and OpenHarmony platform, respectively.

[0010] Preferably, the browser engine implementation classes include the WebViewInMac class instantiated on the macOS platform; the CCefView class instantiated on the OpenHarmony platform; and the QCefView class instantiated on the Windows / Linux platform.

[0011] Preferably, it also includes a standardized error handling module; The standardized error handling module is used to intercept the onLoadError event of browser engines on various platforms and dynamically generate a responsive HTML error page with a unified style to override the default error message.

[0012] Preferably, the unified API interface includes: URL navigation: loadUrl(), the Q_OS_DARWIN macro calls the loadRequest function, other macros call the navigateToUrl function; HTML content loading: loadHTMLString(), the Q_OS_DARWIN macro calls the loadString function, other macros call the navigateToString function; Page control: Unified encapsulation of page interactions such as reload() and closeWebView().

[0013] According to the present invention, a cross-platform WebView encapsulation method based on conditional compilation is provided, employing the aforementioned cross-platform WebView encapsulation device based on conditional compilation. The encapsulation method includes: Step S1: Construct a three-layer calling architecture consisting of a WebView encapsulation layer, a platform adaptation layer, and an underlying platform layer, and define the function signature of a unified API interface; Step S2: During the compilation phase, the platform adaptation layer determines the target operating system through Qt predefined macros; Step S3: After the WebView wrapper layer receives the unified API called by the developer, the platform adaptation layer maps the API to the corresponding function of the underlying platform layer according to the current platform type; Step S4: When the browser engine of the underlying platform layer triggers the onLoadError event, the error handling module intercepts the original error information, dynamically generates a uniform HTML error page, and calls the page loading function of the current platform to overwrite the default error page.

[0014] Preferably, step S2 includes: If the Q_OS_DARWIN macro is detected, the instantiation code block of the WebViewInMac class is activated; If the Q_OS_OPENHARMONY macro is detected, the instantiation code block of the CCefView class is activated; If the Q_OS_WIN or Q_OS_LINUX macro is detected, the instantiation block of the QCefView class is activated.

[0015] Preferably, in step S3: URL navigation: loadUrl(), the Q_OS_DARWIN macro calls the loadRequest function, other macros call the navigateToUrl function; HTML content loading: loadHTMLString(), the Q_OS_DARWIN macro calls the loadString function, other macros call the navigateToString function; Page control: Unified encapsulation of page interactions such as reload() and closeWebView().

[0016] Preferably, the uniformly formatted HTML error page includes an error code, a description text, and a retry button, and adopts a responsive design.

[0017] Compared with the prior art, the present invention has the following beneficial effects: 1. This invention digests platform differences at the underlying level by establishing a unified encapsulation layer and API interface, so that the upper-layer business code can be "written once and run anywhere".

[0018] 2. This invention ensures consistency of core interactions and visual feedback across all platforms through a standardized error handling mechanism. Attached Figure Description

[0019] Other features, objects, and advantages of the present invention will become more apparent from the following detailed description of non-limiting embodiments with reference to the accompanying drawings: Figure 1 This is a schematic diagram of the system structure of the present invention; Figure 2 This is a schematic diagram of the working method of the present invention. Detailed Implementation

[0020] The present invention will now be described in detail with reference to specific embodiments. These embodiments will help those skilled in the art to further understand the present invention, but do not limit the invention in any way. It should be noted that those skilled in the art can make several changes and improvements without departing from the concept of the present invention. These all fall within the protection scope of the present invention.

[0021] This invention utilizes Qt preprocessor macros (Q_OS_DARWIN, Q_OS_OPENHARMONY, Q_OS_WIN, etc.) for compile-time platform detection, dynamically selecting and instantiating the corresponding browser engine implementation (WebViewInMac, CCefView, QCefView) for different operating systems.

[0022] According to the present invention, a cross-platform WebView wrapper based on conditional compilation is provided, such as... Figure 1The diagram shows three layers: the WebView wrapper layer, the platform adaptation layer, and the underlying platform layer. Communication between these three layers follows a top-down call flow. The relationship between the WebView wrapper layer and the platform adaptation layer is a simple function call. The WebView wrapper layer provides a unified API interface as the entry point for developers to directly call, shielding the differences between the underlying platforms. The relationship between the platform adaptation layer and the underlying platform layer is a native API call determined at compile time. After receiving instructions from the upper layer, the platform adaptation layer executes the code blocks activated by conditional compilation. Specifically, the platform adaptation layer uses Qt preprocessor macros (Q_OS_WIN, Q_OS_DARWIN, Q_OS_OPENHARMONY) during the compilation phase to detect the operating system. Based on the detection results, it dynamically selects and instantiates the underlying browser view that best matches the platform, as detailed below: macOS platform: Use the Q_OS_DARWIN macro to detect and instantiate the WebViewInMac class. OpenHarmony platform: Detection using the Q_OS_OPENHARMONY macro, instantiating the CCefView class. Windows / Linux platforms: Use the Q_OS_WIN or Q_OS_LINUX macros for detection, and the QCefView class. The WebView wrapper layer provides a unified interface layer for WebView implementations on different platforms, as detailed below: URL navigation: loadUrl(), the Q_OS_DARWIN macro calls the loadRequest function, other macros call the navigateToUrl function. HTML content loading: loadHTMLString(), the Q_OS_DARWIN macro calls the loadString function, other macros call the navigateToString function. Page control: Unified encapsulation of page interactions such as reload() and closeWebView() The cross-platform WebView wrapper based on conditional compilation also includes a standardized error handling module. This module intercepts loading error events (onLoadError) from browser engines on various platforms and dynamically generates responsive HTML error pages with a unified style to override default error messages, providing users with a consistent and more user-friendly error feedback experience across all platforms.

[0023] This invention aims to solve the technical problems of inconsistent APIs, significant behavioral differences, and inconsistent error feedback in browser controls during cross-platform development by providing a unified API interface layer, standardized error handling mechanism, temporary file management, and event notification mechanism.

[0024] According to the present invention, a cross-platform WebView encapsulation method based on conditional compilation is provided, such as... Figure 2 As shown, the cross-platform WebView encapsulation device based on conditional compilation employs the encapsulation method including: Step S1: Construct a three-layer calling architecture consisting of a WebView encapsulation layer, a platform adaptation layer, and an underlying platform layer, and define the function signature of a unified API interface.

[0025] Step S2: During the compilation phase, the platform adaptation layer determines the target operating system using Qt predefined macros. Specifically, if the Q_OS_DARWIN macro is detected (macOS platform), the instantiation code block of the WebViewInMac class is activated; if the Q_OS_OPENHARMONY macro is detected (OpenHarmony platform), the instantiation code block of the CCefView class is activated; if the Q_OS_WIN or Q_OS_LINUX macro is detected (Windows / Linux platform), the instantiation code block of the QCefView class is activated.

[0026] Step S3: After the WebView wrapper layer receives the unified API called by the developer, the platform adaptation layer maps the API to the corresponding function in the underlying platform layer according to the current platform type. Specifically, the loadUrl function is mapped to WebViewInMac::loadRequest under the Q_OS_DARWIN macro, and to QCefView::navigateToUrl or CCefView::navigateToUrl under other macros; the loadHTMLString function is mapped to WebViewInMac::loadString under the Q_OS_DARWIN macro, and to QCefView::navigateToString or CCefView::navigateToString under other macros; functions such as reload and closeWebView are mapped to the corresponding implementations of each platform according to similar rules.

[0027] Step S4: When the browser engine of the underlying platform layer triggers the onLoadError event (such as a network error or invalid URL), the error handling module intercepts the original error information, dynamically generates a uniform HTML error page with a responsive design, and calls the page loading function of the current platform to override the default error page.

[0028] Those skilled in the art will understand that, besides implementing the system and its various devices, modules, and units provided by this invention in the form of purely computer-readable program code, the same functions can be achieved entirely through logical programming of the method steps, making the system and its various devices, modules, and units of this invention function in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers. Therefore, the system and its various devices, modules, and units provided by this invention can be considered as a hardware component, and the devices, modules, and units included therein for implementing various functions can also be considered as structures within the hardware component; alternatively, the devices, modules, and units for implementing various functions can be considered as both software modules implementing the method and structures within the hardware component.

[0029] Specific embodiments of the present invention have been described above. It should be understood that the present invention is not limited to the specific embodiments described above, and those skilled in the art can make various changes or modifications within the scope of the claims, which do not affect the essence of the present invention. Unless otherwise specified, the embodiments and features described in this application can be arbitrarily combined with each other.

Claims

1. A cross-platform WebView wrapper based on conditional compilation, characterized in that, include: WebView encapsulation layer, platform adaptation layer, and underlying platform layer; The WebView encapsulation layer provides a unified API interface as an entry point for developers to directly call, shielding the differences between underlying platforms; The platform adaptation layer uses Qt preprocessor macros to detect the target operating system type during the compilation stage, and dynamically selects and instantiates the underlying browser view that best matches the platform based on the detection results. The underlying platform layer includes browser engine implementation classes specific to each platform.

2. The cross-platform WebView encapsulation device based on conditional compilation according to claim 1, characterized in that, The platforms include: Windows / Linux platform, macOS platform, and OpenHarmony platform.

3. The cross-platform WebView encapsulation device based on conditional compilation according to claim 2, characterized in that, The preprocessor macros include Q_OS_WIN or Q_OS_LINUX, Q_OS_DARWIN, and Q_OS_OPENHARMONY, which correspond to the Windows / Linux platform, the macOS platform, and the OpenHarmony platform, respectively.

4. The cross-platform WebView encapsulation device based on conditional compilation according to claim 2, characterized in that, The browser engine implementation classes include the WebViewInMac class for macOS; the CCefView class for OpenHarmony; and the QCefView class for Windows / Linux.

5. The cross-platform WebView encapsulation device based on conditional compilation according to claim 1, characterized in that, It also includes a standardized error handling module; The standardized error handling module is used to intercept the onLoadError event of browser engines on various platforms and dynamically generate a responsive HTML error page with a unified style to override the default error message.

6. The cross-platform WebView encapsulation device based on conditional compilation according to claim 1, characterized in that, The unified API interface includes: URL navigation: loadUrl(), the Q_OS_DARWIN macro calls the loadRequest function, other macros call the navigateToUrl function; HTML content loading: loadHTMLString(), the Q_OS_DARWIN macro calls the loadString function, other macros call the navigateToString function; Page control: Unified encapsulation of page interactions such as reload() and closeWebView().

7. A cross-platform WebView encapsulation method based on conditional compilation, characterized in that, The cross-platform WebView encapsulation device based on conditional compilation as described in any one of claims 1 to 6, wherein the encapsulation method includes: Step S1: Construct a three-layer calling architecture consisting of a WebView encapsulation layer, a platform adaptation layer, and an underlying platform layer, and define the function signature of a unified API interface; Step S2: During the compilation phase, the platform adaptation layer determines the target operating system through Qt predefined macros; Step S3: After the WebView wrapper layer receives the unified API called by the developer, the platform adaptation layer maps the API to the corresponding function of the underlying platform layer according to the current platform type; Step S4: When the browser engine of the underlying platform layer triggers the onLoadError event, the error handling module intercepts the original error information, dynamically generates a uniform HTML error page, and calls the page loading function of the current platform to overwrite the default error page.

8. The cross-platform WebView encapsulation method based on conditional compilation according to claim 7, characterized in that, Step S2 includes: If the Q_OS_DARWIN macro is detected, the instantiation code block of the WebViewInMac class is activated; If the Q_OS_OPENHARMONY macro is detected, the instantiation code block of the CCefView class is activated; If the Q_OS_WIN or Q_OS_LINUX macro is detected, the instantiation block of the QCefView class is activated.

9. The cross-platform WebView encapsulation method based on conditional compilation according to claim 7, characterized in that, In step S3: URL navigation: loadUrl(), the Q_OS_DARWIN macro calls the loadRequest function, other macros call the navigateToUrl function; HTML content loading: loadHTMLString(), the Q_OS_DARWIN macro calls the loadString function, other macros call the navigateToString function; Page control: Unified encapsulation of page interactions such as reload() and closeWebView().

10. The cross-platform WebView encapsulation method based on conditional compilation according to claim 7, characterized in that, The standardized HTML error page includes an error code, a description text, and a retry button, and adopts a responsive design.

Citation Information

Patent Citations

  • Communication API (Application Program Interface) framework and method based on Android WebView encapsulation

    CN114546352A

  • A Method and System for Cross-Platform Exception Detection and Repair Based on WebView

    CN119783118B

  • Dynamic webpage data acquisition method based on WebKit browser engine

    CN102214098A

  • Qt-based cross-multi-platform instant messaging method and system

    CN111865756A

  • Mobile terminal cross-platform development system and method based on browser kernel

    CN114489681A

Cited By

  • Cross-platform sky map component two-way communication method and device based on message queue

    CN121396962A