Implementing Core Browser Features in HarmonyOS using Web Components
This demonstration illustrates how to construct a basic browser application within HarmonyOS. It utilizes the @ohos.systemparameter interface alongside the native Web component to enable core functionalities such as rendering web content and managing the navigation history stack.
Application Usage
- Ensure an active Wi-Fi connection is established before launching the application to view the default homepage.
- Navigate to specific websites by either clicking icons on the default page or entering a URL manually into the input field and triggering the navigation action.
- Traverse the browsing history using the directional controls located near the address bar to move forward or backward through previously visited pages.
- Return to the startup screen via the home icon, or initiate a new browsing session by tapping the add button.
Project Structure
The source code is organized to separate concerns between the UI, logic, and utility layers:
entry/src/main/ets/
├── Application/
│ └── AbilityStage.ets // Application entry point
├── pages/
│ └── Index.ets // Main UI layout
├── common/
│ ├── PhoneLayout.ets // Window management utilities
│ └── TitleBar.ets // Navigation bar component
└── model/
├── Logger.ts // Logging mechanism
└── Browser.ets // Core browser logic and instance
Technical Implementation
The browser's navigation and rendering logic are primarily encapsulated within the Browser module.
Page Loading and Refreshing: The WebController API is central to the operation. To display web content, the loadUrl() method is invoked with the target address. For updating the current view, the controller's refresh() method is utilized.
History Stack Navigation: Implementing forward and backward navigation requires checking the history state before execution. The controller provides accessForward() and accessBackward() methods to verify if corresponding history entries exist. Once confirmed, the actual navigation is performed using the forward() or backward() commands.
Required Permissions
To function correctly, the application requires the following permissions in the configuration file:
ohos.permission.INTERNETohos.permission.GET_WIFI_INFO
Constraints and Requirements
- This implementation is designed for standard system devices only.
- Interaction requires an external pointing device (mouse) for verification.
- The project is compatible with API version 9 (SDK version 3.2.11.9).
- Tab switching functionality is not supported in this version.
- Accessing the
@ohos.systemparametersystem interface requires the Full SDK; compilation will fail with the public SDK. - Development requires DevEco Studio 3.1 Beta2 (Build Version: 3.1.0.400) or later.