鸿蒙 应用内三种方式拉起应用市场
在应用开发中推荐其他应用或展示当前应用的详情页是一个常见需求。通过展示应用详情页面用户可以直达应用市场简化下载流程增加应用的下载量和用户活跃度。一、方式选择场景推荐方式应用内打开应用市场loadProduct方式Web页面打开应用市场App Linking方式二、限制限制项说明模拟器不支持提示无法获取内容请点击屏幕重试设备支持Phone、Tablet、PC/2in1、TV(22)三、方式一loadProduct接口调用3.1 接口接口描述loadProduct(context, want, callback)加载应用详情页面接口3.2 开发步骤步骤1导入模块import { productViewManager } from kit.AppGalleryKit; import { hilog } from kit.PerformanceAnalysisKit; import type { common, Want } from kit.AbilityKit; import { BusinessError } from kit.BasicServicesKit;步骤2构造应用详情页参数Entry Component struct LoadProductView { build() { Column() { Button(拉起应用市场详情页) .fontSize(24) .fontWeight(FontWeight.Bold) .onClick(() { // 归因数据可选 const exposureData: productViewManager.SKExposure { adTechId: 20****e8, campaignId: 123456, destinationId: 10*******, mmpIds: [2f****5, 2f7***5], serviceTag: 123***2, nonce: 123***2, timestamp: 1705536488, signature: MEQCIEQlmZ****zKBSE8QnhLTIHZZZ****ZpRqRxHss65Ko****JgJKjdrWdkL****juEx2RmFS7da****ZRVZ8RyMyUXg }; const uiContext this.getUIContext().getHostContext() as common.UIAbilityContext; const wantParam: Want { parameters: { // 必填要加载的应用包名 bundleName: com.huawei.hmsapp.books, // 可选归因数据 skExposure: exposureData } }; const callback: productViewManager.ProductViewCallback { onError: (error: BusinessError) { hilog.error(0, TAG, loadProduct error: ${error.code}, ${error.message}); }, onAppear: () { hilog.info(0, TAG, loadProduct onAppear.); }, onDisappear: () { hilog.info(0, TAG, loadProduct onDisappear.); } }; // 调用loadProduct productViewManager.loadProduct(uiContext, wantParam, callback); }) } .width(100%) .height(100%) .justifyContent(FlexAlign.Center) } }参数说明参数类型必填说明bundleNamestring是要加载的应用包名skExposureSKExposure否归因数据用于统计来源四、方式二Deep Linking方式4.1 链接格式uri: store://appgallery.huawei.com/app/detail?id bundleName4.2 代码示例import { BusinessError } from kit.BasicServicesKit; import { hilog } from kit.PerformanceAnalysisKit; import type { common, Want } from kit.AbilityKit; function startAppGalleryDetailAbility(context: common.UIAbilityContext, bundleName: string): void { let want: Want { action: ohos.want.action.appdetail, // 隐式指定action uri: store://appgallery.huawei.com/app/detail?id bundleName }; context.startAbility(want).then(() { hilog.info(0x0001, TAG, Succeeded in starting Ability successfully.) }).catch((error: BusinessError) { hilog.error(0x0001, TAG, Failed to startAbility. Code: ${error.code}, message: ${error.message}); }); } Entry Component struct StartAppGalleryDetailAbilityView { build() { Column() { Button(拉起应用市场详情页) .onClick(() { const context this.getUIContext().getHostContext() as common.UIAbilityContext; const bundleName com.huawei.hmsapp.books; startAppGalleryDetailAbility(context, bundleName); }) } .width(100%) .height(100%) .justifyContent(FlexAlign.Center) } }4.3 网页中打开Deep Linkinghtml langen head meta charsetUTF-8 /head body div button typebutton onclickopenDeepLink()拉起应用详情页/button /div /body /html script function openDeepLink() { window.open(store://appgallery.huawei.com/app/detail?idcom.xxxx.xxxx) } /script五、方式三App Linking方式5.1 链接格式let link: string https://appgallery.huawei.com/app/detail?id bundleName;5.2 代码示例import { BusinessError } from kit.BasicServicesKit; import { hilog } from kit.PerformanceAnalysisKit; import type { common } from kit.AbilityKit; Entry Component struct Index { build() { Button(start app linking, { type: ButtonType.Capsule }) .width(87%) .height(5%) .margin({ bottom: 12vp }) .onClick(() { let context this.getUIContext().getHostContext() as common.UIAbilityContext; let bundleName: string com.huawei.hmsapp.books; let link: string https://appgallery.huawei.com/app/detail?id bundleName; // appLinkingOnly: false 表示优先在应用市场打开失败则用浏览器 context.openLink(link, { appLinkingOnly: false }) .then(() { hilog.info(0x0001, TAG, openlink success.); }) .catch((error: BusinessError) { hilog.error(0x0001, TAG, openlink failed. Code: ${error.code}, message: ${error.message}); }); }); } }5.3 网页中打开App Linkinghtml langen head meta charsetUTF-8 title跳转示例/title /head body a hrefhttps://appgallery.huawei.com/app/detail?idcom.xxxx.xxxxAppLinking跳转示例/a /body /html六、三种方式对比方式适用场景优势注意事项loadProduct应用内拉起支持归因回调可监听页面打开/关闭需要传入Want参数Deep Linking应用内/网页格式简单通用性强需要指定actionApp Linking应用内/网页支持降级失败时用浏览器打开appLinkingOnly参数控制行为
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2584183.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!