1. Download Weibo Sharing SDK
- Official SDK Website: https://open.weibo.com/wiki/SDK
- Download the HarmonyOS SDK demo project package. After completion, the compressed file should appear as:
Insert image description here
2. Modify Project Configuration and Signing
- After extracting the package, open the project in DevEco Studio and update the configuration:
- The downloaded project may use outdated signing settings and SDK references, requiring manual adjustments to run successfully.
- Cleanup Steps:
- Delete the
oh-package-lock.json5
file. - Remove SDK reference paths in
local.properties
.
Insert image description here - Delete unused core module references in
build-profile.json5
. Re-sync the signing configuration using automatic signing.
Insert image description here - Ensure
oh-package.json5
devDependencies match your IDE and SDK versions.
3. Run the Weibo SDK Project and Integrate Sharing Code
Insert image description here
The demo project demonstrates SDK initialization and sharing functions. Follow these steps for integration:
3.1 Obtain the Weibo SDK
Insert image description here
3.2 Initialize the Weibo SDK
Initialize via the WBAPI singleton and track the status:
// Index.ets private initSdk() { this.isInit = true; this.mWBAPI = WBAPI.getInstance(); }
3.3 Weibo Sharing API Implementation
The sharing page (SharePage.ets
) supports three types: text, images, and videos, similar to Android/iOS:
// SharePage.ets private async doWeiboShare() { const message: WeiboMultiMessage = new WeiboMultiMessage(); // 1. Text sharing const textObject = new TextObject(); let text = "I'm sharing text via Weibo client."; if (this.shareText) { text = "Set your sharing content here!"; textObject.text = text; message.textObject = textObject; } // 2. Multi-image sharing if (this.shareMultiImage) { const multiImage = new MultiImageObject(); try { const uris = new ArrayList<string>(); uris.add(fileUri.getUriFromPath(Utils.getImageCacheFile(getContext()) + "/aaa.png")); // Add more image URIs... multiImage.uriStrs = uris.convertToArray(); } catch (e) { const err = e as BusinessError; Utils.logger.error("zhaojun8", "Multi-image file operation failed: " + err.message); } message.multiImageObject = multiImage; } // 3. Video sharing if (this.shareVideo) { const videoObj = new VideoSourceObject(); try { const videoFilePath = Utils.getVideoCacheFile(getContext()) + '/eeee.mp4'; const videoUri = fileUri.getUriFromPath(videoFilePath); const coverUri = fileUri.getUriFromPath(Utils.getImageCacheFile(getContext()) + '/cover.png'); videoObj.videoPath = videoUri; videoObj.coverPath = coverUri; } catch (e) { const err = e as BusinessError; Utils.logger.error("zhaojun8", "Video file operation failed: " + err.message); } message.videoSourceObject = videoObj; } // Execute sharing if (this.mWBAPI) { const listener: WbASListener = { onComplete: () => promptAction.showToast({ message: 'Sharing succeeded', duration: 2000 }), onError: (error: UiError) => promptAction.showToast({ message: 'Sharing failed: ' + error.errorMessage, duration: 2000 }), onCancel: () => promptAction.showToast({ message: 'Sharing canceled', duration: 2000 }) }; this.mWBAPI.shareMessage(this.context, message, listener); } }
Sharing Workflow
- After clicking "Share," the app redirects to the Weibo sharing page.
Insert image description here - Grant permission to redirect to Weibo (requires the Weibo app installed and logged in).
Insert image description here - The sharing result (success/cancel/error) is prompted via a toast.
Key Notes
- SDK Version Compatibility: Ensure the downloaded SDK matches your IDE version.
- File Paths: Use valid URIs for images/videos (e.g., sandbox paths).
- Error Handling: Implement robust error callbacks for network or permission issues.
Integrating Weibo sharing into HarmonyOS apps is straightforward—start building share functionality today!