js与native交互

UIWebView

Native调用JS,使用stringByEvaluatingJavaScriptFromString来解释执行js脚本。

//script即为要执行的js方法名称
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

PS:苹果推荐iOS8之后的app使用WKWebView来代替UIWebView,同时也使用方法evaluateJavaScript:completionHandler:来替代stringByEvaluatingJavaScriptFromString,因为会一直等待stringByEvaluatingJavaScriptFromString方法执行完毕

JS调用Native,需要通过UIWebView拦截跳转请求并判断请求头是否是协商好的协议头来判断是否是JS发起调用Native的请求。

- webView:shouldStartLoadWithRequest:navigationType:

function callNative{
    var url = ”callNative://nslogHelloWord“
     window.location.href = url;
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
    //callNative 为约定好的协议头,如果是,则页面不进行跳转
    if ([request.URL.scheme isEqualToString:@"callNative"]) {
        NSlog(@"Hello World");
        return NO;
    }
    //不是自定义协议头,跳转页面
    return YES;
}

WKWebView

苹果自iOS8、OS X 10.10推荐使用WKWebView来代替原先的UIWebViewWKWebView拥有更快的加载速度和性能,更低的内存占用,同时更细致的代理分类来满足不同需要的调用。

Native调用JS,使用方法evaluateJavaScript:completionHandler:来解释执行脚本。

//javaScriptString要执行的js方法
//completionHandler执行结束回调或者发生错误时回调
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^)(id, NSError *error))completionHandler;

JS调用Native,通过代理WKScriptMessageHandler来实现调用。

#import "WKWVController.h"
#import 
@interface WKWVController ()

@property (strong, nonatomic) WKWebView *webView;

@end

@implementation WKWVController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // js配置
    WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    //监听方法名
    [userContentController addScriptMessageHandler:self name:@"jsCallOC"];
    
    // WKWebView的配置
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    configuration.userContentController = userContentController;

    
    // 显示WKWebView
    _webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:configuration];
    _webView.UIDelegate = self; // 设置WKUIDelegate代理
    [self.view addSubview:_webView];
    

    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:urlRequest];
    
    
}


// WKScriptMessageHandler protocol?

- (void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message
{
    NSLog(@"方法名:%@", message.name);
    NSLog(@"参数:%@", message.body);
    // 方法名
    NSString *methods = [NSString stringWithFormat:@"%@", message.name];
    SEL selector = NSSelectorFromString(methods);
    // 调用方法
    if ([self respondsToSelector:selector]) {
        [self performSelector:selector withObject:message.body];
    } else {
        NSLog(@"未实行方法:%@", methods);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)jsCallOC{
    [self.webView evaluateJavaScript:@"wkNativeCallJS('嘿嘿')" completionHandler:^(id _Nullable data, NSError * _Nullable error) {
        NSLog(@"我执行完毕");
    }];
}



    
        
            网页
    
    

        

        
    
        
        function wkNativeCallJS(info){
            //wkwebview 不直接使用js的alert 显示不了
            //http://stackoverflow.com/questions/34185339/wkwebview-javascript-confirm-and-alert-not-working
            console.log(info);
        }
        
    
      function wkWebViewTestClick(){
            // 复杂数据
            var list = [1,2,3];
            var dict = {"name":"阳君", "qq":"937447974"};
            // JS通知WKWebView,jsCallOC即为在controller中注册的方法名称
            window.webkit.messageHandlers.jsCallOC.postMessage(dict);
        }
        
        

        
    


JavaScriptCore方式相互调用

在iOS7.0中苹果公司增加了JS利器JavaScriptCore框架,JavaScriptCore提供了很多灵活的本地OC与JS的交互方式,通过JSContextJSValue来完成的,JSContext是一个WebView中js代码运行环境,js可以通过JSContextblock和通过继承JSExport代理,自定义实现代理方法。

#import "WVController.h"
#import 

@protocol JSObjcDelegate 

-(void)logInfo:(NSString*)info;

@end

@interface WVController ()
@property (weak, nonatomic) IBOutlet UIWebView *wv;
@property (nonatomic, strong) JSContext *jsContext;
@end

@implementation WVController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString* path = [[NSBundle mainBundle]pathForResource:@"index" ofType:@"html"];
    NSURL* url = [NSURL URLWithString:path];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    
    [self.wv loadRequest:request];
   
}


//oc调用js
- (IBAction)callJSMethod:(id)sender {
    [self.wv stringByEvaluatingJavaScriptFromString:@"wvNativeCallJS()"];
}

//使用url拦截
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if ([request.URL.scheme isEqualToString:@"callnative"]) {
        NSLog(@"hello world");
        return false;
    }
    return true;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //获取上下文对象
    self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext[@"Objc"] = self;
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息:%@", exceptionValue);
    };
    
    //直接使用block方式执行oc方法
    self.jsContext[@"logHello"] = ^(){
        NSLog(@"hello");
    };
}
//使用代理接口方式执行native方法
-(void)logInfo:(NSString *)info{
    NSLog(@"js传来的参数%@",info);
    JSValue* callback = self.jsContext[@"wvNativeCallJS"];
    [callback callWithArguments:nil];
}
@end



    
        
            网页
    
    
        
//1
      
//2


    
        
    


DEMO:下载Demo

参考:

  • js(javascript)与OC(Objective-C)交互
  • iOS中JavaScript 与OC交互
  • WebKit(WKScriptMessageHandler)
  • IOS 原生与HTML交互

你可能感兴趣的:(js与native交互)