【HarmonyOS 5】Integrating Weibo Sharing into HarmonyOS Applications

简介: 【HarmonyOS 5】Integrating Weibo Sharing into HarmonyOS Applications1. Download Weibo Sharing SDK


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

  1. 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.
  1. 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

  1. After clicking "Share," the app redirects to the Weibo sharing page.
    Insert image description here
  2. Grant permission to redirect to Weibo (requires the Weibo app installed and logged in).
    Insert image description here
  3. 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!

目录
相关文章
|
4月前
|
JSON 算法 开发工具
HarmonyOS NEXT实战:通过QQ分享内容
本教程介绍如何在HarmonyOS Next项目中接入QQ SDK实现分享功能,包含依赖配置、签名生成及分享逻辑代码,适用于教育类应用的内容分享场景。
224 0
|
4月前
|
物联网 开发工具
【HarmonyOS】鸿蒙应用蓝牙功能实现 (二)
【HarmonyOS】鸿蒙应用蓝牙功能实现 (二)
129 9
【HarmonyOS】鸿蒙应用蓝牙功能实现 (二)
|
4月前
|
JavaScript 开发工具 开发者
【HarmonyOS 5】鸿蒙Web组件和内嵌网页双向通信DEMO示例
【HarmonyOS 5】鸿蒙Web组件和内嵌网页双向通信DEMO示例
133 3
|
4月前
|
传感器 安全 物联网
【HarmonyOS 5】鸿蒙分布式协同应用开发详解
为什么需要分布式协同应用? 首先是因为当今社会,围绕电子产品生态,人们迫切希望,周边的电子设备可以协同操作。例如手机,手表,电视机,汽车,甚至是各种家电产品。 从2015年到如今,手机和pc等老牌电子产品的设备数趋于稳定,其他IoT设备稳步增长。可见人均所拥有的的电子产品的个数,在迅速增加。
198 0
|
4月前
|
存储 安全 API
【HarmonyOS 5】鸿蒙应用隐私保护详解
【HarmonyOS 5】鸿蒙应用隐私保护详解
185 1
|
4月前
|
开发工具 开发者
【HarmonyOS 5】鸿蒙中的UIAbility详解(二)
singleton(单实例模式),说人话就是单例模式,App任务进度中该UIAbilty只能存在一个。 multiton(多实例模式),说人话就是单例模式,App任务进度中该UIAbilty能存在多个。 specified(指定实例模式),这玩意就有点复杂了,参见下图,主要通过唯一标识key来作为判断量,看该UIAbility是创建新的,还是使用已创建的。
179 0
|
4月前
|
消息中间件 物联网 开发工具
【HarmonyOS 5】鸿蒙中如何使用MQTT
MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是一种轻量级、基于发布 / 订阅(Publish/Subscribe)模式的即时通讯协议,专为资源受限的物联网(IoT)设备和低带宽、高延迟或不可靠网络环境设计。
297 0
|
4月前
|
存储 IDE 定位技术
【HarmonyOS 5】鸿蒙组件&模板服务详解 - 助力高效开发的利器
在移动应用开发领域,效率与质量始终是开发者追求的核心目标。鸿蒙系统作为新兴的操作系统,为开发者提供了丰富且强大的开发资源,其中鸿蒙组件&模板服务更是成为开发者快速构建高质量应用的得力助手。
153 0
|
4月前
|
人工智能 自然语言处理 IDE
【HarmonyOS 5】鸿蒙CodeGenie AI辅助编程工具详解
1、CodeGenie是什么? CodeGenie (代码精灵)作为鸿蒙DevEco IDE自带的AI辅助编码工具。
212 0
|
缓存 数据安全/隐私保护 JavaScript
【HarmonyOS 5】鸿蒙页面和组件生命周期函数
【HarmonyOS 5】鸿蒙页面和组件生命周期函数
206 0