import android.webkit.WebView;import android.webkit.WebSettings;import android.webkit.WebViewClient;import android.webkit.WebChromeClient;
<uses-permission android:name="android.permission.INTERNET"/><application android:usesCleartextTraffic="true">...</application>
<WebViewandroid:id="@+id/webview"android:layout_height="match_parent"android:layout_width="match_parent"/>
import android.webkit.JavascriptInterface;public class JsBridge {@JavascriptInterfacepublic void getData(String data) {System.out.println(data);}}
public class MainActivity extends AppCompatActivity {private WebView webview;private WebSettings webSettings;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {webview = (WebView) findViewById(R.id.webview);webSettings = webview.getSettings();webSettings.setUseWideViewPort(true);webSettings.setLoadWithOverviewMode(true);// 禁用缓存webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);webview.setWebViewClient(new WebViewClient(){@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}});// 开启js支持webSettings.setJavaScriptEnabled(true);webview.addJavascriptInterface(new JsBridge(), "jsBridge");// 也可以加载本地html(webView.loadUrl("file:///android_asset/xxx.html"))webview.loadUrl("https://x.x.x/x/");}}
#import <WebKit/WebKit.h>
-(WKWebView *)webView{if(_webView == nil){//创建网页配置对象WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];// 创建设置对象WKPreferences *preference = [[WKPreferences alloc]init];//设置是否支持 javaScript 默认是支持的preference.javaScriptEnabled = YES;// 在 iOS 上默认为 NO,表示是否允许不经过用户交互由 javaScript 自动打开窗口preference.javaScriptCanOpenWindowsAutomatically = YES;config.preferences = preference;//这个类主要用来做 native 与 JavaScript 的交互管理WKUserContentController * wkUController = [[WKUserContentController alloc] init];//注册一个name为jsToOcNoPrams的js方法 设置处理接收JS方法的对象[wkUController addScriptMessageHandler:self name:@"jsToOcNoPrams"];[wkUController addScriptMessageHandler:self name:@"jsToOcWithPrams"];config.userContentController = wkUController;_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) configuration:config];// UI 代理_webView.UIDelegate = self;// 导航代理_webView.navigationDelegate = self;//此处即需要渲染的网页NSString *path = [[NSBundle mainBundle] pathForResource:@"JStoOC.html" ofType:nil];NSString *htmlString = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];[_webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];}return _webView;}[self.view addSubview:self.webView];
// 页面开始加载时调用-(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {}// 页面加载失败时调用-(void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {[self.progressView setProgress:0.0f animated:NO];}// 当内容开始返回时调用-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {}// 页面加载完成之后调用-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {[self getCookie];}//提交发生错误时调用-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {[self.progressView setProgress:0.0f animated:NO];}// 接收到服务器跳转请求即服务重定向时之后调用-(void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {}
<p style="text-align:center"> <button id="btn2" type = "button" onclick = "jsToOcFunction()"> JS调用OC:带参数 </button> </p>function jsToOcFunction(){window.webkit.messageHandlers.jsToOcWithPrams.postMessage({"params":"res.randstr"});}
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{//此处message.body即传给客户端的json数据//用message.body获得JS传出的参数体NSDictionary * parameter = message.body;//JS调用OCif([message.name isEqualToString:@"jsToOcWithPrams"]){//在此处客户端得到js透传数据 并对数据进行后续操作parameter[@"params"]}}
本页内容是否解决了您的问题?