【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!

目录
相关文章
|
6月前
|
JSON 算法 开发工具
HarmonyOS NEXT实战:通过QQ分享内容
本教程介绍如何在HarmonyOS Next项目中接入QQ SDK实现分享功能,包含依赖配置、签名生成及分享逻辑代码,适用于教育类应用的内容分享场景。
302 0
|
6月前
|
编解码 区块链 数据安全/隐私保护
格式工厂下载免费版,格式工厂电脑版下载,格式工厂官方下载
格式工厂是一款功能强大的多媒体格式转换工具,支持几乎所有类型视频、音频和图片格式的转换。视频可转为MP4、3GP、AVI等;音频可转为MP3、WMA、FLAC等;图片可转为JPG、PNG等。同时支持DVD/CD抓取、多媒体文件修复与压缩、GPU加速编码等功能,适配iPhone、PSP等多种设备。软件绿色免费,操作简便,是目前使用最广泛的格式转换工具之一。
316 3
|
搜索推荐 数据挖掘 BI
问卷调查软件精选,效能与易用双赢
本文推荐了四款问卷调查软件:ZohoSurvey、SurveyMonkey、GoogleForms和Typeform。ZohoSurvey功能全面,性价比高,适合中小企业和个人;SurveyMonkey功能强大,适合大型企业和科研机构;GoogleForms免费易用,适合预算有限的用户;Typeform注重用户体验,适合需要高参与率的调查。选择时需综合考虑功能、易用性和品牌信任度等因素。
361 2
|
前端开发
OAuth2 完成用户登录【详解】(含码云 gitee 的实现范例)
OAuth2 完成用户登录【详解】(含码云 gitee 的实现范例)
1071 4
|
SQL 大数据 分布式数据库
SQL与大数据的神秘力量:如何用高效SQL处理海量数据,让你的项目一鸣惊人?
【8月更文挑战第31天】在现代软件开发中,处理海量数据是关键挑战之一。本文探讨了SQL与大数据结合的方法,包括数据类型优化、索引优化、分区优化及分布式数据库应用,并通过示例代码展示了如何实施这些策略。通过遵循最佳实践,如了解查询模式、使用性能工具及定期维护索引,开发者可以更高效地利用SQL处理大规模数据集。随着SQL技术的发展,其在软件开发中的作用将愈发重要。
511 0
|
小程序 前端开发 Java
基于微信小程序的电影院订票选座系统的设计与实现(程序+数据库+)
基于微信小程序的电影院订票选座系统的设计与实现(程序+数据库+)
|
SQL 监控 大数据
DataGrip 面试题及答案整理,最新面试题
DataGrip 面试题及答案整理,最新面试题
423 0
|
传感器 编解码 算法
ALOHA协议仿真模拟
用python实现纯ALOHA协议和时隙ALOHA协议仿真。
579 1
ALOHA协议仿真模拟
|
NoSQL Redis 数据库
Redis内存分析方法
线上经常遇到用户想知道自己Redis实例内存使用情况,质疑内存占用量太高。为了不影响线上实例的使用,我们一般会采用bgsave生成dump.rdb文件,再结合redis-rdb-tools和sqlite来进行静态分析。
20461 0
|
计算机视觉
OpenCV-绘制旋转矩形
OpenCV-绘制旋转矩形
383 0