Cross-platform webview encapsulation device and method based on conditional compilation
The cross-platform WebView wrapper with conditional compilation solves the problem of inconsistent WebView APIs across platforms, providing a unified API interface and standardized error handling, and enabling code reuse and a consistent interactive experience in cross-platform development.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-22
- Publication Date
- 2026-03-24
AI Technical Summary
Cross-platform WebView implementations suffer from issues such as inconsistent APIs, significant behavioral differences, and inconsistent error feedback. Existing technologies, such as CN114546352A and CN119783118B, have failed to effectively resolve the compatibility and reusability issues of cross-platform WebView.
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 operating system is detected through 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.
It achieves consistent core interactions and visual feedback across different platforms, improving code reusability and development efficiency, and ensuring uniformity in error feedback.
Smart Images

Figure CN120994177B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of communication, in particular to a cross-platform WebView encapsulation device and method based on conditional compilation. BACKGROUND
[0002] With the rise of various PC operating systems, especially the development of domestic Xinchuang system and Hongmeng operating system, cross-platform development has become the trend. Among many cross-platform solutions, Qt framework has become one of the preferred tools for developers with its powerful C++ library, rich components and good support for desktop and embedded systems. At the same time, displaying web content within the application has also become a basic function. WebView, as the core component to carry web content, has a huge difference in implementation mechanism on different platforms, which brings serious challenges to cross-platform development. To solve this problem, the present application proposes a cross-platform WebView encapsulation device and method based on conditional compilation, which shields the differences of various platforms through unified interface calling and improves code reuse rate. This method dynamically enables the WebView implementation module of the corresponding platform according to the platform identifier in the compilation environment, thereby realizing adaptation on multiple operating systems such as Windows, macOS and Hongmeng.
[0003] Patent document CN119783118B discloses a cross-platform exception detection and repair method and system based on WebView, which initializes the WebView component, loads the H5 page and injects multiple listeners to detect the running of the H5 page. When any of the listeners triggers an exception, the system immediately refreshes the H5 page, automatically skips the exception function or component, and uploads the exception data to the server. The server classifies and analyzes the exception data, identifies the exception problem and automatically generates a dynamic repair patch, which is injected into the H5 page to realize rapid repair of the exception problem. This method can realize accurate detection and positioning of cross-platform exceptions, and intelligently filter and recover the page. However, patent document CN119783118B focuses on exception detection and repair in cross-platform applications, rather than fundamentally solving the problem of non-uniform cross-platform WebView API.
[0004] Patent document CN114546352A discloses a communication API framework and method based on Android WebView encapsulation, including: WebView, JSAPI and Java API encapsulation class; the WebView communicates with the JSAPI, the JSAPI communicates with the Java API encapsulation class; the WebView directly calls the Java API encapsulation class through the JSAPI, so that the Java API encapsulation class establishes communication with the terminal device to obtain JSAPI data.
[0005] But the patent document CN114546352A has platform limitations, the whole technical architecture is deeply bound to the Android system, lacks portability and scalability, and cannot form a unified development paradigm. The invention solves the compatibility and reuse problems between multiple platforms. SUMMARY
[0006] In view of the defects in the prior art, the purpose of the present application is to provide a cross-platform WebView encapsulation device and method based on conditional compilation.
[0007] The cross-platform WebView encapsulation device based on conditional compilation provided by the present application comprises a WebView encapsulation layer, a platform adaptation layer and a bottom platform layer.
[0008] The WebView encapsulation layer provides a unified API interface as an entry for direct calls by developers, shielding the differences between the bottom platforms.
[0009] The platform adaptation layer uses Qt preprocessor macros to detect the target operating system type at the compilation stage, and dynamically selects and instantiates the most suitable bottom browser view for the corresponding platform according to the detection results.
[0010] The bottom platform layer includes browser engine implementation classes specific to each platform.
[0011] Preferably, the platforms include Windows / Linux platforms, macOS platforms and OpenHarmony platforms.
[0012] Preferably, the preprocessor macros include Q_OS_WIN or Q_OS_LINUX, Q_OS_DARWIN and Q_OS_OPENHARMONY corresponding to Windows / Linux platforms, macOS platforms and OpenHarmony platforms.
[0013] Preferably, the browser engine implementation classes include WebViewInMac class instantiated for macOS platforms, CCefView class instantiated for OpenHarmony platforms and QCefView class instantiated for Windows / Linux platforms.
[0014] Preferably, it further comprises a standardized error handling module.
[0015] The standardized error handling module is used to intercept the load error event onLoadError of each platform browser engine, and dynamically generate a responsive HTML error page of a unified style to cover the default error prompt.
[0016] Preferably, the uniform API interface comprises:
[0017] URL navigation: loadUrl(), calling the loadRequest function under the Q_OS_DARWIN macro, and calling the navigateToUrl function under other macros;
[0018] HTML content loading: loadHTMLString(), calling the loadString function under the Q_OS_DARWIN macro, and calling the navigateToString function under other macros;
[0019] Page control: reload(), closeWebView(), and other uniform encapsulation of page interaction.
[0020] According to the cross-platform WebView encapsulation method based on conditional compilation provided by the application, the cross-platform WebView encapsulation device based on conditional compilation is adopted, and the encapsulation method comprises:
[0021] Step S1: constructing a three-layer calling architecture of a WebView encapsulation layer, a platform adaptation layer, and a bottom platform layer, and defining the function signature of a uniform API interface;
[0022] Step S2: in the compilation stage, the platform adaptation layer judges the target operating system through a Qt predefined macro;
[0023] Step S3: after the WebView encapsulation layer receives the uniform API called by the developer, the platform adaptation layer maps the API to the corresponding function of the bottom platform layer according to the current platform type;
[0024] Step S4: when the browser engine of the bottom platform layer triggers an onLoadError event, the error processing module intercepts the original error information, dynamically generates an HTML error page in a uniform format, and calls the page loading function of the current platform to cover the default error page.
[0025] Preferably, the step S2 comprises:
[0026] If the Q_OS_DARWIN macro is detected, the instantiation code block of the WebViewInMac class is activated;
[0027] If the Q_OS_OPENHARMONY macro is detected, the instantiation code block of the CCefView class is activated;
[0028] If the Q_OS_WIN or Q_OS_LINUX macro is detected, the instantiation code block of the QCefView class is activated.
[0029] Preferably, the step S3 includes:
[0030] URL navigation: loadUrl(), calling loadRequest function under Q_OS_DARWIN macro, calling navigateToUrl function under other macros;
[0031] HTML content loading: loadHTMLString(), calling loadString function under Q_OS_DARWIN macro, calling navigateToString function under other macros;
[0032] Page control: reload(), closeWebView() and other unified encapsulation of page interaction.
[0033] Preferably, the unified format of HTML error page includes error code, description text and retry button, and the style adopts responsive design.
[0034] Compared with the prior art, the application has the beneficial effects as follows:
[0035] 1. The application establishes a unified encapsulation layer and API interface, digests platform differences in the bottom layer, so that the upper business code can be "written once and run everywhere".
[0036] 2. The application standardizes the error processing mechanism, and ensures the consistency of core interaction and visual feedback on all platforms. BRIEF DESCRIPTION OF DRAWINGS
[0037] Other features, objects and advantages of the application will become more apparent through reading the following detailed description of non-limiting embodiments with reference to the accompanying drawings:
[0038] Figure 1 It is a system structure schematic diagram of the application;
[0039] Figure 2 It is a working method flowchart schematic diagram of the application. DETAILED DESCRIPTION
[0040] The application will be described in detail below with reference to specific embodiments. The following embodiments will help those skilled in the art to further understand the application, but do not limit the application in any form. It should be pointed out that, for those skilled in the art, without departing from the concept of the application, a number of changes and improvements can be made. These all belong to the protection scope of the application.
[0041] 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.
[0042] According to the present invention, a cross-platform WebView wrapper based on conditional compilation is provided, such as... Figure 1 The 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:
[0043] macOS platform: Use the Q_OS_DARWIN macro to detect and instantiate the WebViewInMac class.
[0044] OpenHarmony platform: Detection using the Q_OS_OPENHARMONY macro, instantiating the CCefView class.
[0045] Windows / Linux platforms: Use the Q_OS_WIN or Q_OS_LINUX macros for detection, and the QCefView class.
[0046] The WebView wrapper layer provides a unified interface layer for WebView implementations on different platforms, as detailed below:
[0047] URL navigation: loadUrl(), the Q_OS_DARWIN macro calls the loadRequest function, other macros call the navigateToUrl function.
[0048] HTML content loading: loadHTMLString(), the Q_OS_DARWIN macro calls the loadString function, other macros call the navigateToString function.
[0049] Page control: Unified encapsulation of page interactions such as reload() and closeWebView()
[0050] 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.
[0051] 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.
[0052] 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:
[0053] 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.
[0054] 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.
[0055] 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.
[0056] 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.
[0057] 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.
[0058] 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; 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; 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().
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. 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 4, 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 in the underlying platform layer according to the current platform type; 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(); 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.
6. The cross-platform WebView encapsulation method based on conditional compilation according to claim 5, 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.
7. The cross-platform WebView encapsulation method based on conditional compilation according to claim 5, 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