乐闻世界logo
搜索文章和话题

How to avoid Wechat warning about visiting an external page?

1个答案

1

WeChat Mini Program enforces strict restrictions on accessing external links within the mini program to enhance user experience and security. However, in certain business scenarios, it may be necessary to direct users to external websites. To avoid pop-up warnings, the following strategies can be employed:

1. Using WeChat's Official Components

WeChat Mini Program provides the component, which allows developers to embed external web pages directly within the mini program. Using this component loads web content without triggering security warnings. However, this feature is only available if the domain to be accessed is added to the business domain list in the WeChat Public Platform backend.

Example: If your mini program requires users to view a news article, you can load the news page using the component, provided that the news website's domain has been added to the mini program's business domain list.

2. Using WeChat Open Tags

WeChat Mini Program supports using the open-type="navigate" attribute within the <navigator> component for page navigation. When redirecting to external links, setting open-type="navigate" ensures that clicking the link does not trigger a warning.

Example:

xml
<navigator url="https://www.example.com" open-type="navigate">Access External Link</navigator>

3. Using API Interfaces

For cases requiring data retrieval from external sources, the mini program's backend server can fetch external data using methods like CURL, and then pass the data to the frontend via mini program APIs, bypassing direct access restrictions.

Example:

  • Backend server uses CURL or similar methods to retrieve external data.
  • Pass the retrieved data to the mini program via its API interfaces.

4. User Education and Guidance

In some cases, if the above methods are not applicable, user guidance within the mini program can inform users how to open links in a browser. For example, providing a button to copy the link, which users can then manually open in their browser.

Example:

xml
<button data-url="https://www.example.com" bindtap="copyLink">Copy Link</button>

In the mini program, handle the copy functionality:

javascript
copyLink: function(event) { wx.setClipboardData({ data: event.currentTarget.dataset.url, success: function() { wx.showToast({ title: 'Link copied', icon: 'success', duration: 2000 }); } }); }

Summary

The above methods effectively prevent security warnings when accessing external links in WeChat Mini Programs. The choice of method depends on specific business requirements and implementation complexity. By appropriately utilizing WeChat Mini Program components and APIs, developers can provide rich content and a seamless user experience while adhering to WeChat platform rules.

2024年6月29日 12:07 回复

你的答案