提交 7e39e8ad 编写于 作者: E ester.zhou

Update docs (17556)

Signed-off-by: Nester.zhou <ester.zhou@huawei.com>
上级 6cb42a52
# FAQs
- [Full SDK Compilation](full-sdk-compile-guide.md)
- [Switching to Full SDK](full-sdk-switch-guide.md)
- [Ability Development](faqs-ability.md)
- [ArkUI Development](faqs-arkui.md)
- [ArkUI Development (ArkTS Syntax)](faqs-arkui-arkts.md)
- [Web Development](faqs-arkui-web.md)
- [Bundle Management Development](faqs-bundle-management.md)
- [Resource Manager Development](faqs-globalization.md)
- [Common Event and Notification Development](faqs-event-notification.md)
......@@ -16,4 +21,5 @@
- [Pan-Sensor Development](faqs-sensor.md)
- [Startup Development](faqs-startup.md)
- [Distributed Device Development](faqs-distributed-device-profile.md)
- [SDK Usage](faqs-sdk.md)
- [Usage of Third- and Fourth-Party Libraries](faqs-third-fourth-party-library.md)
\ No newline at end of file
此差异已折叠。
# Web Development
## How does the return result of onUrlLoadIntercept affect onInterceptRequest in the \<Web> component?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
The operation that follows **onUrlLoadIntercept** is subject to its return result.
- If **true** is returned, the URL request is intercepted.
- If **false** is returned , the **onInterceptRequest** callback is performed.
**Reference**
[onUrlloadIntercept](../reference/arkui-ts/ts-basic-components-web.md#onurlloadinterceptdeprecated)
## What should I do if the onKeyEvent event of the \<Web> component is not triggered as expected?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Problem**
The **onKeyEvent** event is set for the **\<Web>** component to listen for keyboard events. However, it is not triggered when a key is pressed or lifted.
**Solution**
Currently, the **\<Web>** component does not support the **onKeyEvent** event. To listen for keyboard events for the **\<Web>** component, you can use the **onInterceptKeyEvent** callback function.
**Reference**
[onInterceptKeyEvent](../reference/arkui-ts/ts-basic-components-web.md#oninterceptkeyevent9)
## What should I do if page loading fails when onInterceptRequest is used to intercept URLs and return the custom HTML file?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Problem**
The **onInterceptRequest** API intercepts URLs specified by **src** and returns the custom HTML file. However, the content in the **script** tag in the HTML file is not loaded.
**Solution**
If only **setResponseData** is set for the interceptor, the kernel cannot identify the HTML file. You must also set parameters such as **setResponseEncoding**, **setResponseMimeType**, and **setResponseHeader** for the kernel to identify the HTML file.
**Example**
```
Web({ src: 'www.example.com', controller: this.controller })
.onInterceptRequest((event) => {
console.log('url:' + event.request.getRequestUrl())
this.responseweb = new WebResourceResponse();
var head1:Header = {
headerKey:"Connection",
headerValue:"keep-alive"
}
var length = this.heads.push(head1)
this.responseweb.setResponseHeader(this.heads)
this.responseweb.setResponseData(this.webdata)
this.responseweb.setResponseEncoding('utf-8')
this.responseweb.setResponseMimeType('text/html')
this.responseweb.setResponseCode(200)
this.responseweb.setReasonMessage('OK')
return this.responseweb
})
```
**Reference**
[WebResourceResponse](../reference/arkui-ts/ts-basic-components-web.md#webresourceresponse)
## How do I execute JS functions in HTML in ArkTS code?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
Use the **runJavaScript** API in **WebviewController** to asynchronously execute JavaScript scripts and obtain the execution result in a callback.
>**NOTE**
>
>**runJavaScript** can be invoked only after l**oadUrl** is executed. For example, it can be invoked in **onPageEnd**.
**Reference**
[runJavaScript](../reference/apis/js-apis-webview.md#runjavascript)
## How do I invoke an ArkTS method on a local web page loaded in the \<Web> component?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
1. Prepare an HTML file. Below is the sample code:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Title</h1>
<h5 id="h5"></h5>
<h5 id = "h6"></h5>
<button onclick="handleFromH5">Invoke ArkTS method </button>
<script type="text/javascript">
function handleFromH5(){
let result = window.objName.test();
document.getElementById('h6').innerHTML = result;
}
</script>
</body>
</html>
```
2. Use the **JavaScriptProxy** API in ArkTs to register the object in ArkTS with the window object of H5, and then use the window object to call the method in H5. In the following example, the **testObj** object in ArkTS is registered with the window object of H5 with the alias **objName**. In H5, **window.objName** can then be used to access the object.
```
// xxx.ets
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
controller: web_webview.WebviewController = new web_webview.WebviewController()
testObj = {
test: (data1, data2, data3) => {
console.log("data1:" + data1);
console.log("data2:" + data2);
console.log("data3:" + data3);
return "AceString";
},
toString: () => {
console.log('toString' + "interface instead.");
}
}
build() {
Row() {
Column() {
Web({ src:$rawfile('index.html'), controller:this.controller })
.javaScriptAccess(true)
.javaScriptProxy({
object: this.testObj,
name: "objName",
methodList: ["test", "toString"],
controller: this.controller,
})
}
.width('100%')
}
.height('100%')
}
}
```
**Reference**
[javaScriptProxy](../reference/arkui-ts/ts-basic-components-web.md#javascriptproxy)
## How do I set the domStorageAccess attribute of the \<Web> component?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
The **domStorageAccess** attribute sets whether to enable the DOM Storage API. By default, this feature is disabled.
**Reference**
[domStorageAccess](../reference/arkui-ts/ts-basic-components-web.md#domstorageaccess)
## What should I do if the network status fails to be detected on the HTML page loaded by the \<Web> component?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Problem**
When **window.navigator.onLine** is used on the HTML page to obtain the network status, the value is **false** no matter the network connection is set up or not.
**Solution**
Configure the permission for the application to obtain network information: **ohos.permission.GET\_NETWORK\_INFO**.
**Reference**
[GET\_NETWORK\_INFO](../security/permission-list.md#ohospermissionget_network_info)
## How do I set the UserAgent parameter through string concatenation?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
By default, the value of **UserAgent** needs to be obtained through the WebviewController. Specifically, it is obtained by calling the **getUserAgent** API in a **WebviewController** object after it is bound to the **\<Web>** component. Therefore, to set **UserAgent** through string concatenation before page loading:
1. Use @State to define the initial **UserAgent** parameter and bind it to the **\<Web>** component.
2. In the **onUrlLoadIntercept** callback of the **\<Web>** component, use **WebviewController.getUserAgent\(\)** to obtain the default **UserAgent** value and update the UserAgent bound to the **\<Web>** component.
**Example**
```
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct Index {
private controller: web_webview.WebviewController = new web_webview.WebviewController()
@State userAgentPa: string = ''
build() {
Row() {
Column() {
Web({ src: 'http://www.example.com', controller: this.controller }) // Replace the URL with the actual URL.
.width('100%')
.userAgent(this.userAgentPa)
.onUrlLoadIntercept((event) => {
let userAgent = this.controller.getUserAgent();
this.userAgentPa = userAgent + ' 111111111'
return false;
})
}
.width('100%')
}
.height('100%')
}
}
```
**Reference**
[userAgent](../reference/arkui-ts/ts-basic-components-web.md#useragent) and [getUserAgent](../reference/apis/js-apis-webview.md#getuseragent)
## How do I enable the \<Web> component to return to the previous web page following a swipe gesture?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
Override the **onBackPress** API to define the return logic and use **WebviewController** to determine whether to return to the previous web page.
**Example**
```
import web_webview from '@ohos.web.webview';
@Entry
@Component
struct Index {
controller: web_webview.WebviewController = new web_webview.WebviewController();
build() {
Column() {
Web({ src: 'http://www.example.com', controller: this.controller })// Replace the URL with the actual URL.
}
}
onBackPress() {
// Check whether a specific number of steps forward or backward can be performed on the current page. A positive number indicates forward, and a negative number indicates backward.
if (this.controller.accessStep(-1)) {
this.controller.backward(); // Return to the previous web page.
// Execute the custom return logic.
return true
} else {
// Execute the default return logic to return to the previous page.
return false
}
}
}
```
**Reference**
[Web](../reference/apis/js-apis-webview.md#accessstep)
此差异已折叠。
# Common Event and Notification Development
# Event and Notification Development
## What is the emitter data size limit?
## How do I encapsulate a commonEvent utility class?
Applicable to: OpenHarmony SDK 3.2.5.3, stage model of API version 9
Applicable to: OpenHarmony 3.1 Beta 5 (API version 9)
The emitter data size cannot exceed 10240.
**Problem**
## How do I implement the click-a-notification-to-open-an-application function?
A commonEvent utility class needs to be encapsulated for the following purpose: Register a custom callback function when creating a subscriber, and then call the custom callback function when receiving an event notification.
Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
**Solution**
You can implement this function by setting the **wantAgent** attribute in the **NotificationRequest** parameter of the **Notification.publish** API.
```
import commonEvent from '@ohos.commonEventManager';
export class SubscribeEvent {
private static subscriber = null
// Custom callback function
private static callback = null
/**
* Create a subscriber.
* @param subscribeInfo Indicates the event to subscribe to.
* @callback Indicates the custom callback.
*/
static createSubscriber(subscribeInfo, callback:(a,b)=>void) {
this.callback = callback
commonEvent.createSubscriber(subscribeInfo, (err, subscriber) => {
if (err) {
console.error('CreateSubscriberCallBack err = ' + JSON.stringify(err))
} else {
this.subscriber = subscriber;
this.subscribe(this.subscriber)
console.info('Create subscriber succeed')
}
})
}
Reference: [Notification](../reference/apis/js-apis-notification.md#notificationpublish) and [WantAgent](../reference/apis/js-apis-app-ability-wantAgent.md)
/**
* Subscribe to a common event.
* @param subscriber Indicates the subscriber.
*/
private static subscribe(subscriber) {
if (subscriber != null) {
commonEvent.subscribe(subscriber, (err, data) => {
if (err) {
console.error('subscribe err = ' + JSON.stringify(err))
} else {
console.info('SubscribeCallBack data= ' + JSON.stringify(data))
this.callback('hello callback', data)
}
})
} else {
console.info("Need create subscriber")
}
}
}
Example:
@Entry
@Component
struct Faq10_1 {
@State message: string = ''
build() {
Row() {
Column() {
Text ('Subscribe:' + this.message)
.fontSize(30)
.fontWeight(FontWeight.Bold)
.onClick(() => {
let subscribeInfo = {
events: ["myEvent"]
};
let callback = (a,b) => {
this.message = a
}
SubscribeEvent.createSubscriber(subscribeInfo,callback)
})
Text ('Publish')
.fontSize(30)
.fontWeight(FontWeight.Bold)
.onClick(() => {
// Attributes of a common event.
let options = {
code: 0, // Result code of the common event.
data: "initial data",// Result data of the common event.
isOrdered: true // The common event is an ordered one.
}
// Callback for common event publication.
function publishCB(err) {
if (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
} else {
console.info("publish");
}
}
// Publish a common event.
try {
commonEvent.publish("myEvent", options, publishCB);
} catch (err) {
console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
}
})
}
.width('100%')
}
.height('100%')
}
}
```
```ts
**Reference**
[@ohos.commonEventManager (Common Event)](../reference/apis/js-apis-commonEventManager.md)
## How do I make events be transferred in only one UIAbility instance?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Problem**
Events need to be subscribed to and triggered only in one UIAbility instance.
**Solution**
Use the API in the **EventHub** module of the UIAbility to subscribe to events. The **EventHub** module offers the event center, which provides the API for subscribing to, unsubscribing from, and triggering events.
**Example**
```
import UIAbility from '@ohos.app.ability.UIAbility';
export default class EntryAbility extends UIAbility {
onForeground() {
this.context.eventHub.on('myEvent', this.eventFunc);
// Result
// eventFunc is called,undefined,undefined
this.context.eventHub.emit('myEvent');
// Result
// eventFunc is called,1,undefined
this.context.eventHub.emit('myEvent', 1);
// Result
// eventFunc is called,1,2
this.context.eventHub.emit('myEvent', 1, 2);
}
eventFunc(argOne, argTwo) {
console.log('eventFunc is called, ${argOne}, ${argTwo}');
}}
```
**Reference**
[Using EventHub for Data Synchronization](../application-models/uiability-data-sync-with-ui.md#using-eventhub-for-data-synchronization).
## How do I implement a click-to-open-application feature in the notification?
Applicable to: OpenHarmony 3.1 Beta 5 (API version 9)
**Solution**
You can implement this feature by setting the **wantAgent** attribute in the **NotificationRequest** parameter of the **Notification.publish** API.
**Example**
```
import notificationManager from '@ohos.notificationManager';
import WantAgent from '@ohos.app.ability.wantAgent';
async function publishNotification() {
let wantAgentInfo = {
wants: [
{
bundleName: "com.example.myapplication",
bundleName: "com.example.webuseragent", // Bundle name of the target application.
abilityName: "EntryAbility",
}
],
operationType: WantAgent.OperationType.START_ABILITIES,
requestCode: 0,
requestCode: 1,
}
const wantAgent = await WantAgent.getWantAgent(wantAgentInfo)
let contentType = Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT;
await Notification.publish({
let contentType = notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT;
await notificationManager.publish({
content: {
contentType: contentType,
normal: {
......@@ -43,6 +188,25 @@ async function publishNotification() {
id: 1,
wantAgent: wantAgent
})
prompt.showToast ({ message: "Sent successfully." })
}
```
**Reference**
[Notification](../reference/apis/js-apis-notificationManager.md) and [WantAgent](../reference/apis/js-apis-app-ability-wantAgent.md)
## What should I do if calling notificationManager.publish fails?
Applicable to: OpenHarmony 3.2 Beta5
**Problem**
After a notification is published, no error log is displayed, and no notification is displayed in the notification panel.
**Solution**
Before publishing a notification, you must enable the notification feature for your application in the system settings of the real device so that the notification can be viewed in the notification panel.
To manually enable the notification feature, choose **Settings** > **Notification & status bar** > *Application name* > **Allow notifications**.
You can also call the **notificationManager.requestEnableNotification\(\)** API to display a dialog box (only once) to prompt the user to enable the feature.
# SDK Usage
## What is the macro definition of the arm64-v8a/armeabi-v7a directory in CMake?
Applicable to: OpenHarmony 3.1 Beta 5 (API version 9)
**Solution**
The **arm64-v8a** and **armeabi-v7a** directories are as follows:
```
entry
├─ libs
│ ├─ arm64-v8a
│ │ └─ libMyDemo.so
│ └─ armeabi-v7a
│ └─ libMyDemo.so
└─ src
└─ main
└─ cpp
└─ CMakeLists.txt
```
The macro for accessing the directory is **\$\{CMAKE\_CURRENT\_SOURCE\_DIR\}/../../../libs/$\{OHOS\_ARCH\}/xxxx.so**.
**CMAKE\_CURRENT\_SOURCE\_DIR**: directory where the **CMakeList.txt** file is stored.
**OHOS\_ARCH**: type of the application binary interface (ABI). The value can be **armeabi-v7a** or **arm64-v8a**. The default value is **arm64-v8a**.
**Example**
Add the link library to **CMakeLists.txt**.
```
target_link_libraries(entry PUBLIC
libace_napi.z.so
libhilog_ndk.z.so
${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${OHOS_ARCH}/libMyDemo.so
)
```
## What should I do if an error is reported when OH\_LOG\_Print in the native code is used to print logs?
Applicable to: OpenHarmony 3.1 Beta 5 (API version 9)
**Problem**
When **OH\_LOG\_Print** is used in the native code to print logs, the following error is reported: **undefined symbol: OH\_LOG\_Print**.
**Cause**
The link library file is missing.
**Solution**
Open the **CMakeLists.txt** file and append **libhilog\_ndk.z.so** to **target\_link\_libraries**.
```
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(entry PUBLIC
libace_napi.z.so
libhilog_ndk.z.so
)
```
## How do I traverse files in rawfile?
Applicable to: OpenHarmony 3.1 Beta 5 (API version 9)
**Solution**
Use the **OH\_ResourceManager\_OpenRawDir\(\)** native API to obtain the root directory of **rawfile** and traverse the root directory.
**Reference**
[Rawfile](../reference/native-apis/rawfile.md)
\ No newline at end of file
# Full SDK Compilation Guide
# Full SDK Compilation
The full SDK provides a full set of APIs available in OpenHarmony, including system APIs required by system applications. Vendors can leverage this SDK to develop applications.
......
# Guide to Switching to Full SDK
# Switching to Full SDK
Both the public SDK and full SDK are toolkits for application development. <br>The public SDK is intended for application developers and provided as standard in DevEco Studio. It does not contain system APIs – APIs required by system applications.
......
# System Common Events
# System Common Events (To Be Deprecated Soon)
This document provides indexes for system common events defined in OpenHarmony.
For details about the definition of a system common event, see [Support in @ohos.commonEvent (Common Event)](./js-apis-commonEvent.md#support).
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册