tencent cloud

web-view 组件中特殊链接处理

PDF
聚焦模式
字号
最后更新时间: 2025-07-04 17:33:29
如果 web-view 组件展示的网页中存在特殊的 URL 链接,例如 tcmpp://host/path 这种以自定义 scheme 开头的链接,superapp可以接管这些链接的跳转操作,并自定义跳转行为。
接管链接跳转应当创建类实现 IWebViewLinkProxy 接口,并添加 ProxyService 注解:
@ProxyService(proxy = IWebViewLinkProxy.class)
public class CustomWebViewLinkProxy implements IWebViewLinkProxy {

/**
* Handle custom url loading in web-view component.
* Custom url is url with any custom scheme (scheme not network protocol such as http/https)
* @param miniContext context of mini-program
* @param url the url which is loading
* @return return true if the url loading is handled, otherwise false the web-view will handle url loading
*/
@Override
public boolean webViewCustomUrlLoading(IMiniAppContext miniContext, String url) {
Uri uri = Uri.parse(url);
if ("tcmpp".equals(uri.getScheme())) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
miniContext.getAttachedActivity().startActivity(intent);
return true;
}
return false;
}
}
实现方法中提供了小程序上下文以及被加载的链接。如果开发者处理了链接跳转并返回 true,web-view 将不再处理该链接。如果返回 false,web-view 将按照正常逻辑处理该链接。


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈