Vue.js与WKWebView交互

0.js点击事件,oc监听事件和参数

onLineCollectionFun:function () {
    console.log('在线收款')
    window.webkit.messageHandlers.OnLineCollectionFun.postMessage({'methodsID':'1'});
},

1.

#import <WebKit/WebKit.h>
@interface GuestHomeController ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>
@property(nonatomic,strong) WKWebView * webView;
@property(nonatomic,weak) MBProgressHUD*hud;
@end

2.

WKWebView*webV=[[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:configuration];
[self.view addSubview:webV];
_webView=webV;
webV.backgroundColor=[UIColor groupTableViewBackgroundColor];
webV.UIDelegate=self;
webV.navigationDelegate=self;
NSString * urlString = @"";
urlString = [kBaseWebURL stringByAppendingString:@"guesthome"];
[webV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];

3.

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = [WKUserContentController new];
[configuration.userContentController addScriptMessageHandler:self name:@"OnLineCollectionFun"];

4.

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    HWDLog(@"%@", message.body);
    HWDLog(@"%@", message.name);
    NSDictionary * body = [message.body objectForKey:@"body"];
    if ([message.name isEqualToString:@"OnLineCollectionFun"]) {
        [self onLineCollectController];
    }
}
阅读更多
文章标签: webview WKWebView
个人分类: iOS vuejs
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭