美文网首页对JScript一点不懂webviewjs交互
iOS_经验(4)_js和oc交互(UI&WKWebVi

iOS_经验(4)_js和oc交互(UI&WKWebVi

作者: 丶纳凉 | 来源:发表于2016-07-12 17:21 被阅读1062次

一丶说明

推荐使用WKWebView

在性能、稳定性
WKWebView更多的支持HTML5的特性
WKWebView更快,占用内存可能只有UIWebView的1/3 ~ 1/4
WKWebView高达60fps的滚动刷新率和丰富的内置手势
WKWebView具有Safari相同的JavaScript引擎
WKWebView增加了加载进度属性
将UIWebViewDelegate和UIWebView重构成了14个类与3个协议

WKWebView的学习:http://www.jianshu.com/p/7bb5f15f1daa

二丶 iOS调用js

WKWebView

用到方法:
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ __nullable)(__nullable id, NSError * __nullable error))completionHandler;

例子:调用js的hello()方法,传递"我是被iOS调用"的参数

iOS方面;
[self.webView evaluateJavaScript:@"hello('我是被iOS调用的')" completionHandler:nil];

js方面

function hello(msg)
{
        document.write(msg);
}

UIWebView

iOS方法
   [self.webView stringByEvaluatingJavaScriptFromString:@"hello('我是被iOS调用的')"];
js:
 function hello(msg) {
   document.write(msg)
}

三丶js调用iOS

WKWebView

用到方法:
- (void)addScriptMessageHandler:(id <WKScriptMessageHandler>)scriptMessageHandler name:(NSString *)name;
例子:js点击按钮,调用iOS的hello_OC方法;传递参数:{hello:1};

js代码:

<input type="button" value="点击触发OC方法" onclick="window.webkit.messageHandlers.hello_OC.postMessage({hello:1})">

oc代码:
1.代理
<WKScriptMessageHandler>
2.注入对象/方法
  WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
//这个方法会造成代理不会因为页面消失而释放:
// [config.userContentController addScriptMessageHandler:self name:@"hello_OC"];
使用(WeakScriptMessageDelegate 已经封装好了,可以直接使用)
  [config.userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"hello_OC"];

self.webView = [[WKWebView alloc] initWithFrame:CGRectZero
                                      configuration:config];

3.
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message
{
    NSLog(@"<JS调用了 %@ 方法,参数:%@>", message.name, message.body);
}
4.WeakScriptMessageDelegate

#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>

@interface WeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>

@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;

- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;

@end

@implementation WeakScriptMessageDelegate

- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate
{
    self = [super init];
    if (self) {
        _scriptDelegate = scriptDelegate;
    }
    return self;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    [self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}

@end

UIWebView

iOS方法:

#pragma mark - delegete
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"__>%s",__func__);
    NSLog(@"__>%@",request.URL.absoluteString);
    NSString *parameterStr = [[request.URL.absoluteString componentsSeparatedByString:@"parm:"] lastObject];
    NSDictionary *dict  = [parameterStr ZB_dictionaryWithURLString];
    NSLog(@"js调用oc,参数为:%@",dict);
    
    return YES;
}

js方面:
function toOC(argument) {
            /*UIWebVIew*/
            window.location.href = "parm:hello iOS"
}

function iOSShareApp(argument) {
            /*UIWebView*/
            window.location.href = "parm:shareTitle=hyd&shareDesc='副标题'&shareUrl='分享跳转的链接'&sharePic='www.hyd.com'"
        }

四丶注意

五丶github

https://github.com/k373379320/OCAndJs

成熟第三方:
https://github.com/marcuswestin/WebViewJavascriptBridge

相关文章

  • iOS_经验(4)_js和oc交互(UI&WKWebVi

    一丶说明 二丶 iOS调用js WKWebView UIWebView 三丶js调用iOS WKWebView U...

  • OC与JS交互

    OC与JS交互前言 OC与JS交互之UIWebView OC与JS交互之WebViewJavascriptBrid...

  • OC与JS交互总结

    OC与JS交互的4种方式 1 UIWebView 1.1 直接URL拦截 JS调OC shouldStartLoa...

  • OC和JS交互(UIWebView)中级篇2

    上回书说 OC和JS交互的一些准备工作, 下面开始OC和JS交互的重头戏->JS调用OC. 这里我们会用到Safa...

  • OC和JS交互、JS和OC交互

    现在做开发 很多会出现交互问题 我在公司项目中也会用到交互 下面我大致写下 交互的代码 - (void)loadW...

  • iOS 的JS 和 OC 交互(二)

    iOS 的JS 和 OC 交互(一) 好吧 今天继续再来搞搞 这个 交互的问题 OC 怎样可以拦截到 JS 的调...

  • WKWebView和WebView与JS的交互方式

    WKWebView和WebView与JS的交互方式 UIWebView与JS的交互方式 一、 OC调用JS 直接调...

  • ios 开发OC 和 JS 交互

    ios 开发OC 和 JS 交互 最近遇到了关于 oc 和 js 交互的功能, 记录一下就当是是做了笔记的 开发环...

  • iOS 中的JS交互

    在iOS的webview开发中,都会用到和HTML交互。一般JS的交互一般有几种: OC调用JS JS调用OC 动...

  • oc 与js 的原生交互

    参考 总评: oc 与js的交互,1.有原生的方式,oc 调js简单,js调oc 麻烦(协议拦截"实现的交互方式)...

网友评论

    本文标题:iOS_经验(4)_js和oc交互(UI&WKWebVi

    本文链接:https://www.haomeiwen.com/subject/wycsjttx.html