webView怎么实现JS调原生(html使用window.iosDelegate.方法名(参数)调用原生方法)

具体操作 如下:
1、创建一个UIViewController 如:PublicServerViewController

2、引入 #import

3、代理 < UIWebViewDelegate>

4、声明@property (nonatomic ,strong) UIWebView * webView;
@property (nonatomic, strong) JSContext * jsContext;

5、初始化UIWebView

- (UIWebView *)webView {
   if (!_webView) {
      _webView = [[UIWebView alloc] initWithFrame:RECT(0, heightWebView + 44, SCREENWIDTH, SCREENHEIGHT - (heightWebView) - 44)];
      _webView.delegate = self;
      _webView.opaque = NO;
      _webView.backgroundColor = [UIColor whiteColor];
      if (@available(ios 11.0,*)){ _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;}
   }
   return _webView;
}

6、实现UIWebViewDelegate

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
     return YES;
 }
 
 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
 }
 
 - (void)webViewDidStartLoad:(UIWebView *)webView {
 }
 
 - (void)webViewDidFinishLoad:(UIWebView *)webView {
 }

7、下面是window.iosDelegate.方法名(参数)调用原生方法处理,iosDelegate可以换成和后台约定的

 //获取JS代码的执行环境/上下文/作用域
 self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
 //! 在context注册iOSDelegate对象为self
 self.jsContext[@"iosDelegate"] = self;//注意循环引用问题

8、 声明js调用原生的方法

 @protocol JSDelegate 
 - (void)getName:(id)parameter;
 @end

9、给PublicServerViewController 引入协议 JSDelegate

10、实现getName方法 实现想要的操作

 //修改名字
 - (void)getTitle:(id)parameter {
     dispatch_async(dispatch_get_main_queue(), ^{
     });
 }

具体代码实现:

#import "PublicServerViewController.h"
#import 
@protocol JSDelegate 
- (void)getName:(id)parameter;
@end
@interface PublicServerViewController ()
@property (nonatomic ,strong) UIWebView * webView;
@property (nonatomic, strong) JSContext * jsContext;
@end

@implementation PublicServerViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSString stringWithFormat:@"" ]];
    [self.webView loadRequest:request];
}

- (UIWebView *)webView {
   if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:RECT(0, heightWebView + 44, SCREENWIDTH, SCREENHEIGHT - (heightWebView) - 44)];
        _webView.delegate = self;
        _webView.opaque = NO;
        _webView.backgroundColor = [UIColor whiteColor];
        if (@available(ios 11.0,*)){ _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;}
    }
    return _webView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

- (void)dealloc {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [self.webView stopLoading];
    self.webView.delegate = nil;
    self.webView = nil;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.jsContext = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext[@"iosDelegate"] = self;
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息:%@", exceptionValue);
    };
    self.jsContext[@"console"][@"log"] = ^(JSValue * msg) {
        LHLog(@"H5  log : %@", msg);
    };
    NSString * title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if (![@"" isEqualToString:title]) {
        self.title = title;
    }
}

//修改Name
- (void)getName:(id)parameter {
    dispatch_async(dispatch_get_main_queue(), ^{
    });
}

@end

你可能感兴趣的:(ios随笔,JSExport)