ios与html5交互 简书,IOS与H5交互(WKWebView)

先上H5代码

未设置密码/忘记密码?

点击我跳转链接

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/';

}

继续OC代码

.h

#import

.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"可以获取到"未设置密码/忘记密码?"点击事件

}

}

你可能感兴趣的:(ios与html5交互,简书)