环境
IOS
小程序使用组件web-view
单页面应用
现象:使用小程序web-view组件的时候,会发现在ios中打开内嵌SAP时,会发现在分享的时候,有时拿到的webViewUrl并不是当前页面的url。
解决方法
其实解决方法也很简单,从web-view开放能力中,我们能找到这样的接口:
wx.miniProgram.postMessage 向小程序发送消息 通过该接口,我们就能在分享的时候,将需要分享的链接,传入到分享中去。
使用方法
# 小程序-wxml
<web-view src="{{source}}" bindmessage="bindmessage"></web-view>
# 小程序-js
/**
* 绑定从分享处传过来的信息
* @param {*} e
*/
bindmessage(e) {
let data = e.detail.data;
let index = data.length - 1;
this.setData({
from: data[index].from,
to: data[index].to,
shareTitle:
data[index].shareTitle == undefined ? "" : data[index].shareTitle,
attach: data[index].attach
});
}
/**
* 配置通用分享消息
*/
onShareAppMessage(options) {
let shareTitle = this.data.shareTitle;
if (shareTitle === undefined || shareTitle == "") {
shareTitle = "xxxxxx";
}
let from = encodeURIComponent(this.data.from);
let to = encodeURIComponent(this.data.to);
let attach = encodeURIComponent(this.data.attach);
return {
title: shareTitle,
path:
"/pages/index/index?from=" + from + "&to=" + to + "&attach=" + attach
};
}
SPA页面
//此处可以传入当前url的链接,或者自己根据相关业务逻辑传入所需参数信息
wx.miniProgram.postMessage({
data: {
from: "aaa",
to: "bbbb",
attach: "a=1&b=3",
shareTitle: "shareTitle"
}
});
就这么简单~