Javascript 用于移动网站调用iOS/Android系统共享对话框的共享按钮
有没有可能在我的网站上创建一个共享按钮(链接),可以在iOS和Android系统中调用共享对话框 我指的是每个系统的以下对话框: 我不是在问如何使用iOS/Android SDK实现这一点。我只希望它带有HTML/JavaScript。是的,这是可能的。 这里是它的链接 首先创建html页面,并将下面的代码放入一个按钮中Javascript 用于移动网站调用iOS/Android系统共享对话框的共享按钮,javascript,android,html,ios,Javascript,Android,Html,Ios,有没有可能在我的网站上创建一个共享按钮(链接),可以在iOS和Android系统中调用共享对话框 我指的是每个系统的以下对话框: 我不是在问如何使用iOS/Android SDK实现这一点。我只希望它带有HTML/JavaScript。是的,这是可能的。 这里是它的链接 首先创建html页面,并将下面的代码放入一个按钮中 <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')"
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
现在创建一个类,它将侦听您的回调
上面一行addJavascriptInterface()负责回调
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
记住字符串区分大小写,android需要@JavascriptInterface接口来调用android功能使用通过Javascript在iOS或android上共享URL。
它得到了
- 及
- iOS 12.2被告知将很快(2019年)增加对它的支持
$(“#回答示例共享按钮”)。在('单击',()=>{
if(navigator.share){
navigator.share({
标题:“Web共享API草稿”,
文字:“看看这个规范!”,
网址:'https://wicg.github.io/web-share/#share-方法',
})
。然后(()=>console.log('Successful share'))
.catch((错误)=>console.log('error sharing',error));
}否则{
console.log('此浏览器不支持共享,请按旧方法执行');
}
});代码>
分享代码>这只是Android。这不是HTML/javascript
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}