首先打开webstorm,将最下面h5拷贝到html中。然后导入工程

#define kMessageHandlerName @"mymobile"

1.创建配置类

- (WKWebView *)webView{
if (!_webView) {

WKWebViewConfiguration *config =[[WKWebViewConfiguration alloc]init];

config.preferences.javaScriptEnabled = YES;
/** 默认是不能通过JS自动打开窗口的,必须通过用户交互才能打开 */
config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
_webView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:config];

_webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

_webView.allowsBackForwardNavigationGestures = YES;//打开左划回退功能
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
_webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_webView];

}

return _webView;

}

2.加载H5页面

NSURL *path = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
[self.webView loadRequest:[NSURLRequest requestWithURL:path]];

3 注入对象名称 kMessageHandlerName

- (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        [self.webView.configuration.userContentController addScriptMessageHandler:self name:kMessageHandlerName];
}

- (void)viewDidDisappear:(BOOL)animated{
     [super viewDidDisappear:animated];
     [self.webView.configuration.userContentController removeScriptMessageHandlerForName:kMessageHandlerName];
}

/**代理方法
JS通过mymobile发送数据到iOS端时,在代理中接收
var message = {"method":"call","args":"let go"};
window.webkit.messageHandlers.mymobile(注:这里是注入的对象名).postMessage(message);
*/
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{

NSDictionary *dic = message.body;

NSString *method = dic[@"method"];

if ([method isEqualToString:@"call"]) {

[self call];
      }

NSLog(@"dic = %@", dic);
}

- (void)call{

UIAlertController *sheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[sheet addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//调用js
NSString *js = [NSString stringWithFormat:@"gotoCall()"];
[self.webView evaluateJavaScript:js completionHandler:nil];

}]];

[sheet addAction:[UIAlertAction actionWithTitle:@"不要点我" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:sheet animated:YES completion:nil];

}

/**  在JS端调用alert函数时,会触发此代理方法(确认框) */

- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
      [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
      [self presentViewController:alertVC animated:YES completion:nil];

completionHandler();
}

<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
font-size: 40px;
}
</style>
</head>

<body>

<div style=" margin-top: 150px">

<p></p>

<div>
<button id="call">拍照</button>
</div>

<script type="text/javascript">

call.onclick = function() {
var message = {"method":"call","args":"let go"};
window.webkit.messageHandlers.mymobile.postMessage(message);
};

function gotoCall(){
alert('调用了OC中的alert')
}

</script>

</body>

</html>

WKWebView与Js交互的更多相关文章

  1. ios WKWebView 与 JS 交互实战技巧

    一.WKWebView 由于Xcode8发布之后,编译器开始不支持iOS 7了,这样我们的app也改为最低支持iOS 8.0,既然需要与web交互,那自然也就选择使用了 iOS 8.0之后 才推出的新 ...

  2. WKWebView与JS交互,UIWebView+JavascriptCore和JS交互

    最近一直在做有关Swift和JavaScript交互的程序,所以有关UIWebView和WKWebView在使用上的差别在此总结下: UIWebView: (1)创建 var webView: UIW ...

  3. WKwebView与JS交互(h5主动)

    先:WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler // 创建一个webiview的配置项 WKWebViewConfiguratio ...

  4. WKWebView与js交互中产生的内存泄漏

    最近开发中突然发现富文本帖子详情内存没有释放掉,找了好久问题都没找到,终于今天发现了问题,先上一点代码片段 WKWebViewConfiguration *configuration = [[WKWe ...

  5. iOS(WKWebView)OC与JS交互 之三

      随着H5功能愈发的强大,没进行过混合开发的小伙们都不好意思说自己能够独立进行iOS的app开发,在iOS7操作系统下,常用的native,js交互框架有easy-js,WebViewJavascr ...

  6. WKWebView新特性及JS交互

    引言 一直听说WKWebView比UIWebView强大许多,可是一直没有使用到,今天花了点时间看写了个例子,对其API的使用有所了解,为了日后能少走弯路,也为了让大家更容易学习上手,这里写下这篇文章 ...

  7. iOS(UIWebView 和WKWebView)OC与JS交互 之二

    在iOS应用的开发过程中,我们经常会使用到WebView,当我们对WebView进行操作的时候,有时会需要进行源生的操作.那么我记下来就与大家分享一下OC与JS交互. 首先先说第一种方法,并没有牵扯O ...

  8. UIWebView和WKWebView的使用及js交互

    UIWebView和WKWebView的使用及js交互 web页面和app直接的交互是很常见的东西,之前尝试过flex和js的相互调用以及android和js的相互调用,却只有ios没试过,据说比较复 ...

  9. 【Swift】WKWebView与JS的交互使用

    一.前言 近日,有朋友问我关于WKWebView与JS的交互问题,可我之前一直使用的是UIWebView,也不曾做过WKWebView的交互啊!接下来大家一块学习下WKWebView是怎么实现原生代码 ...

随机推荐

  1. 用border做一个移动端常见的返回按钮

    第一步 .hs1{ float: left; .mt(.25rem); .ml(.12rem); width: .3rem; height: .3rem; border-top: 2px solid ...

  2. CreateProcess函数诡异的表现

    场景:程序A使用CreateProcess函数去启动另一个程序(.exe)文件,在绝大部分情况下是可以成功启动的,但是在某些电脑上无效. 因为这“某些电脑”实在不好找,终于有一天借到一台这样的电脑. ...

  3. Swift2.3适配Swift3.0时出现的各种问题

    昨晚上一波手贱把我的小5s升到iOS10.如此配套的话,Xcode7.3升级Xcode8.1看来也是势在必行了.公司程序是Swift2.3的,出于对苹果的恐惧迟迟不敢升级.但丑媳妇儿总要见公婆,借这个 ...

  4. LWP::UserAgent介绍1

    require LWP::UserAgent; my $ua = LWP::UserAgent->new; ); $ua->env_proxy; my $response = $ua-&g ...

  5. Scope Directive

    ---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...

  6. mysql 分组按条件统计

    百度经验 COUNT(CASE WHEN (S.rank = 1) THEN S.loanContractId END ) AS 'MZ',  //根据loanContractId 分组,并统计ran ...

  7. jquery选择器之基本筛选选择

    1.基本选择器 2.内容筛选选择器 3.可见性筛选选择器 4.属性筛选选择器 5.子元素筛选选择器 6.表单元素选择器 7.表单对象属性筛选器

  8. Linux系统编程初探系列之一:文件编程

    系统函数 int creat(const char* filename,mode_t mode) filename:需要创建的文件名(包含路径,缺省为当前路径) mode:创建模式 常见的创建模式有: ...

  9. 第七十四节,css边框与背景

    css边框与背景 学习要点: 1.声明边框 2.边框样式 3.圆角边框  本章主要探讨HTML5中CSS边框和背景,通过边框和背景的样式设置,给元素增加更丰富的外观. 声明边框 边框的声明有三个属性设 ...

  10. ckplayer 项目实战

    <div class="control-group" id="videoDiv" style="display: none;"> ...