第一、如何忽略https证书校验
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURLCredential *credential = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
}
第二、WebViewJavascriptBridge的使用
OC部分
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import "WKWebViewJavascriptBridge.h"
@interface ViewController ()<WKNavigationDelegate,WKUIDelegate>
@property(strong,nonatomic)WKWebView * webView;
@property(strong,nonatomic)WKWebViewJavascriptBridge * bridge;
@property(strong,nonatomic)UIButton * submitBtn;
@end
@implementation ViewController
-(WKWebView *)webView
{
if (!_webView) {
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
_webView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:config];
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
}
return _webView;
}
-(UIButton *)submitBtn
{
if (!_submitBtn) {
_submitBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 300, 200, 50)];
_submitBtn.backgroundColor = [UIColor greenColor];
[_submitBtn setTitle:@"oc调用js" forState:0];
[_submitBtn addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
}
return _submitBtn;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.webView];
[self.view addSubview:self.submitBtn];
NSString * htmlPath = [[NSBundle mainBundle]pathForResource:@"index.html" ofType:nil];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
//建立桥梁
self.bridge = [WKWebViewJavascriptBridge bridgeForWebView:self.webView];
//打开日志
// [WKWebViewJavascriptBridge enableLogging];
[self registerHandler];
}
#pragma mark - <WKNavigationDelegate>
// 开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
// 可以在这里做正在加载的提示动画 然后在加载完成代理方法里移除动画
}
// 网络错误
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
// 在这里可以做错误提示
}
// 网页加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
// 在这里可以移除正在加载的提示动画
}
//注册原生方法给JS调用
- (void)registerHandler
{
[self.bridge registerHandler:@"jsToOc" handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"js调用oc:%@",data);
responseCallback(@"xxx"); //回调给JS
}];
}
//原生调用JS
-(void)submitAction
{
[self.bridge callHandler:@"ocToJs" data:@"hello" responseCallback:^(id responseData) {
NSLog(@"调用完JS后的回调:%@",responseData);
}];
}
@end
JS部分
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>JS交互</title>
<style>
body {
font-size:30px;
text-align:center;
}
* {
margin: 30px;
padding: 0;
}
h1{
color: red;
}
button{
width: 300px;
height: 50px;
font-size: 30px;
}
</style>
</head>
<body>
<h1>WKWebview与原生交互</h1>
<h2></h2>
<button onclick="alertAction()">js调用oc</button>
</body>
<script>
setupWebViewJavascriptBridge(function(bridge) {
/* Initialize your app here */
bridge.registerHandler('ocToJs', function(data, responseCallback) {
console.log("我从OC那拿到:", data)
responseCallback("我从OC那拿到,现在传回给OC:"+data)
})
})
//固定语句,复制黏贴即可
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge)
{
return callback(WebViewJavascriptBridge);
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback);
}
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
function alertAction(){
window.WebViewJavascriptBridge.callHandler('jsToOc',"js666", function responseCallback(responseData) {
console.log("JS received response:", responseData)
})
}
</script>
</html>
遇到的问题:
第一、pod 之后就报错提示 framework not found Pods 报错
屏幕快照 2019-11-12 上午11.52.36.png
把这个framework文件删除了就编译通过了
网友评论