移动网站的“共享"按钮可调用iOS/Android系统共享对话框 [英] Share button for mobile website to invoke iOS/Android system share dialogs
问题描述
是否可以在我的网站上创建一个可以在iOS和Android系统中调用共享对话框的共享按钮(链接)?
Is it possible to create a share button (link) in my website that can invoke share dialogs in iOS and Android systems?
我的意思是每个系统都有以下对话框:
I mean the following dialog for each system:
我不是在问如何使用iOS/Android SDK.我只想要HTML/JavaScript.
I am not asking of how to do it by using iOS/Android SDKs. I want it with only HTML/JavaScript.
推荐答案
是的,有可能.这是它的链接 https://developer.android.com/guide/webapps/webview.html
Yes, it is possible. Here is the link for it https://developer.android.com/guide/webapps/webview.html
首先创建html页面,然后将下面的代码放入按钮
First create html page and put below code in a button
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
现在,在android中创建一个网页.
Now, create a webpage in android.
WebView myWebView;
WebSettings webSettings;
WebAppInterface webInterface;
myWebView = (WebView) findViewById(R.id.webview);
webInterface=new WebAppInterface(MainActivity.this);
webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(webInterface, "Android");
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.loadUrl("Your url for that html file you created");
现在创建一个将侦听您的回调的类上面的行addJavascriptInterface()负责回调
Now create a class which will listen to your callback above line addJavascriptInterface() is responsible for callback
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功能
Remember String is case sensitive,and android require @JavascriptInterface interface for calling android functionality
这篇关于移动网站的“共享"按钮可调用iOS/Android系统共享对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!