IOS与H5交互(WKWebView)
2020-09-07 本文已影响0人
拖不垮打不烂
先上H5代码
<meta charset="utf-8">
<div style="font-size: 1.6rem;color: rgba(104,187,205,1)">
<span style="margin-right: 15px;" onclick="_password_jump()" >未设置密码/忘记密码?</span>
</div>
<div onclick="window.location.href='https://www.baidu.com'">点击我跳转链接</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
function _password_jump(){
var rd_type = "password";
if(isiOSPlatform()){
var shopping_ios = { name: "view_record",type : rd_type};
window.webkit.messageHandlers.main_onItemClick.postMessage(shopping_ios);
}
}
function isiOSPlatform() {
var ua = navigator.userAgent.toLowerCase();
if (/iphone|ipad|ipod/.test(ua)) {
return true;
} else {
return false;
}
}
function payResult(str){
if(str == "123"){
window.location.href='https://www.hao123.com/';
}
}
window.payResult = function(){
window.location.href='https://www.baidu.com/';
}
</script>
继续OC代码
.h
#import <WebKit/WebKit.h>
<WKNavigationDelegate, WKUIDelegate, WKScriptMessageHandler>
.m
#pragma 初始化WKWebview
-(void)initWebView{
// 创建配置
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
// 创建UserContentController(提供JavaScript向webView发送消息的方法)
_userContent = [[WKUserContentController alloc] init];
// 添加消息处理,注意:self指代的对象需要遵守WKScriptMessageHandler协议,结束时需要移除
[_userContent addScriptMessageHandler:self name:@"main_onItemClick"];
// 将UserConttentController设置到配置文件
config.userContentController = _userContent;
self.WKWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) configuration:config];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",_urlStr]];
NSMutableURLRequest *requ = [NSMutableURLRequest requestWithURL:url];
// 给header添加token
// [requ addValue:UserDefulsGetValue(token) forHTTPHeaderField:@"Authorization"];
self.WKWebView.navigationDelegate = self;
self.WKWebView.UIDelegate = self;
[self.WKWebView loadRequest:requ];
}
#pragma 左滑动返回
-(void)addPopTap{
WeakSelf(self)
[App_Class popOrDissPresentWithView:self.WKWebView block:^{
if (![weakself.WKWebView canGoBack]) {
[weakself.navigationController popViewControllerAnimated:YES];
return;
}
// 传值操作,这里H5执行跳转https://www.hao123.com
NSString * jsStr = [NSString stringWithFormat:@"payResult('%@')",@"123"];
[self.WKWebView evaluateJavaScript:jsStr completionHandler:^(id _Nullable response, NSError * _Nullable error) {
NSLog(@"res:%@----%@",response,error)
}];
}];
// 不传值操作,这里H5执行跳转https://www.baidu.com
[self.WKWebView evaluateJavaScript:@"payResult()" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
NSLog(@"res:%@----%@",response,error)
}];
}];
}
#pragma 链接变动
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSLog(@"urlScheme:%@",navigationAction.request.URL.scheme);
NSLog(@"urlStr:%@",navigationAction.request.URL.absoluteString);
// 这里可以检测到点击"点击我跳转链接" 的navigationAction.request.URL.absoluteString为"https://www.baidu.com"
decisionHandler(WKNavigationActionPolicyAllow);
}
#pragma 点击事件监听
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
NSLog(@"%@---%@", message.name, message.body)
if ([@"main_onItemClick" isEqualToString:message.name]) {
// 通过"main_onItemClick"可以获取到"未设置密码/忘记密码?"点击事件
}
}