iOS_经验(4)_js和oc交互(UI&WKWebVi
2016-07-12 本文已影响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