#鸿蒙通关秘籍#Deep Linking在跳转时,为啥会拉起应用选择框?

HarmonyOS
2024-11-27 14:35:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
数据小鬼才

嗯,Deep Linking在跳转时拉起选择框,是因为匹配到多个应用,系统得让用户选择用哪个。

// 配置代码
{
  "module": {
    // ...
    "abilities": [
      {
        // ...
        "skills": [
          {
            "actions": [
              // actions不能为空,actions为空会造成目标方匹配失败
              "ohos.want.action.viewData"
            ],
            "uris": [
              {
                // scheme必选,可以自定义,以link为例,需要替换为实际的scheme
                "scheme": "link",
                // host必选,配置待匹配的域名
                "host": "www.example.com"
              }
            ]
          }
        ]
      }
    ]
  }
}
// 实现代码
import { common, OpenLinkOptions } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

const TAG: string = '[UIAbilityComponentsOpenLink]';
const DOMAIN_NUMBER: number = 0xFF00;

@Entry
@Component
struct Index {
  build() {
    Button('start link', { type: ButtonType.Capsule, stateEffect: true })
    .width('87%')
    .height('5%')
    .margin({ bottom: '12vp' })
    .onClick(() => {
      let context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
      let link: string = "link://www.example.com";
      let openLinkOptions: OpenLinkOptions = {
        appLinkingOnly: false
      };

      try {
        context.openLink(link, openLinkOptions)
        .then(() => {
          hilog.info(DOMAIN_NUMBER, TAG, 'open link success.');
        }).catch((err: BusinessError) => {
          hilog.error(DOMAIN_NUMBER, TAG, `open link failed. Code is ${err.code}, message is ${err.message}`);
        });
      } catch (paramError) {
        hilog.error(DOMAIN_NUMBER, TAG, `Failed to start link. Code is ${paramError.code}, message is ${paramError.message}`);
      }
    })
  }
}
分享
微博
QQ
微信
回复
2024-11-27 15:40:07
相关问题