JS 与 OC 的 交互
2017-08-01 17:26
113 查看
OC调用JS
方法1:
方法2:
JS调用OC
方法1:
.h文件
方法1:
NSString *jsStr = [NSString stringWithFormat:@"showAlert('%@')",@"这里是JS中alert弹出的message"]; [_webView stringByEvaluatingJavaScriptFromString:jsStr];
方法2:
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; NSString *textJS = @"showAlert('这里是JS中alert弹出的message')"; [context evaluateScript:textJS];
JS调用OC
方法1:
.h文件
#import <UIKit/UIKit.h> #import <JavaScriptCore/JavaScriptCore.h> @protocol JavaScriptDelegate <JSExport> -(void)method:(id)data; @end @interface CustomWebView : UIWebView<JavaScriptDelegate,UIWebViewDelegate> @property (nonatomic,strong)id <JavaScriptDelegate> jsDelegate; @property (nonatomic,strong)JSContext * jsContext; @end.m文件
#import "CustomWebView.h" @implementation CustomWebView -(instancetype)initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { self.delegate=self; } return self; } -(void)setUp{ self.jsContext=[self valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; self.jsContext[@"native"]=self; self.jsDelegate=self; } -(void)method:(id)data{ NSLog(@"%@", data); } -(void)webViewDidFinishLoad:(UIWebView *)webView{ [self setUp]; }
相关文章推荐
- iOS和JS交互(含OC、html、js)代码不使用第三方库(二)
- iOS与JS交互,OC调用JS方法,JS调用OC方法,URL解码
- ios下OC与JS交互之WKWebView
- js与oc 交互
- OC原生代码/webview与js/网页交互的最好方式
- oc与js交互-----WKWebView
- iOS_JS与OC的交互详解
- OC与JS交互
- iOS--js和oc交互
- OC与JS的交互
- JS和OC的交互(基于UIWebView)
- Js与OC交互
- WKWebView OC和JS交互
- OC与JS交互
- OC与js的交互 - javascriptCore
- js(javascript)与OC(Objective-C)交互
- oc与js交互
- iOS中OC与JS交互WebViewJavascriptBridge的简单使用
- iOS - oc与js交互的几种方式(1)
- UIWebView中JS与OC交互 WebViewJavascriptBridge的使用