JSContext这个可是关键。
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
// webView对象
@property (nonatomic, strong, readonly) UIWebView *webView;
@property (nonatomic, strong, readonly) JSContext *jsContext; 下面是在webview加载完成后, 关联JS与OC:
下面是在webview加载完成后, 关联JS与OC:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[_activityView stopAnimating];
[self dismiss];
if (_jsContext == nil) {
// 1.
_jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// 2. 关联打印异常
_jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
DDLogVerbose(@"异常信息:%@", exceptionValue);
};
_jsContext[@"activityList"] = ^(NSDictionary *param) {
DDLogVerbose(@"%@", param);
};
// Mozilla/5.0 (iPhone; CPU iPhone OS 10_10 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B411
id userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
DDLogVerbose(@"%@", userAgent);
}
}
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
// webView对象
@property (nonatomic, strong, readonly) UIWebView *webView;
@property (nonatomic, strong, readonly) JSContext *jsContext; 下面是在webview加载完成后, 关联JS与OC:
下面是在webview加载完成后, 关联JS与OC:
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[_activityView stopAnimating];
[self dismiss];
if (_jsContext == nil) {
// 1.
_jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
// 2. 关联打印异常
_jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
DDLogVerbose(@"异常信息:%@", exceptionValue);
};
_jsContext[@"activityList"] = ^(NSDictionary *param) {
DDLogVerbose(@"%@", param);
};
// Mozilla/5.0 (iPhone; CPU iPhone OS 10_10 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B411
id userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
DDLogVerbose(@"%@", userAgent);
}
}