// // TestViewController.m // QYSDKDemo // // Created by zhongzhida on 2023/4/19. // Copyright © 2023 Netease. All rights reserved. // #import "TestViewController.h" #import @interface TestViewController () @property (nonatomic, strong) WKWebView *webView; @end @implementation TestViewController - (void)viewDidLoad { [super viewDidLoad]; CGRect rect = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)); WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.preferences = [[WKPreferences alloc] init]; config.preferences.minimumFontSize = 10; config.preferences.javaScriptEnabled = YES; config.preferences.javaScriptCanOpenWindowsAutomatically = NO; config.userContentController = [[WKUserContentController alloc] init]; config.processPool = [[WKProcessPool alloc] init]; config.userContentController = [WKUserContentController new]; //在创建wkWebView时,需要将被js调用的方法注册进去,oc与js端对应实现 [config.userContentController addScriptMessageHandler:self name:@"receiveYSFMessage"]; WKWebView *wkWebView = [[WKWebView alloc]initWithFrame:rect configuration:config]; self.webView = wkWebView; wkWebView.navigationDelegate = self; wkWebView.UIDelegate = self; NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://6669.qytest.netease.com/sdk/r/demo-debug.html"]]; [wkWebView loadRequest:request]; [self.view addSubview:wkWebView]; UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; backBtn.frame = CGRectMake(100, 100, 100, 100); [backBtn setTitle:@"返回" forState:UIControlStateNormal]; [self.view addSubview:backBtn]; [backBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; [backBtn addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside]; } - (void)onBack { [self nativeCallJS]; } - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { if ([message.name isEqualToString:@"receiveYSFMessage"]) { NSLog(@"JS调用原生,参数:%@",message.body); [self.navigationController popViewControllerAnimated:YES]; } } - (void)nativeCallJS { [self.webView evaluateJavaScript:@"checkCloseYSFClient()" completionHandler:^(id _Nullable response, NSError * _Nullable error) {   //TODO NSLog(@""); }]; } @end