// // GameListViewController.m // BuguLive // // Created by voidcat on 2023/8/29. // Copyright © 2023 xfg. All rights reserved. // #import "GameListViewController.h" #import "GameViewController.h" @interface GameListViewController () //webview @property(nonatomic, strong) WKWebView *webView; @property (nonatomic, strong) WKUserContentController *userContentC; @end @implementation GameListViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.webView]; NSString *url = [GlobalVariables sharedInstance].appModel.h5_url.game_list; url = [url urlAddCompnentForValue:[IMAPlatform sharedInstance].host.userId key:@"uid"]; url = [url urlAddCompnentForValue:[BogoNetwork shareInstance].token key:@"token"]; [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; [self.webView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(20); make.bottom.equalTo(self.view).offset(-20); make.left.equalTo(self.view); make.right.equalTo(self.view); }]; // self.userContentC = [[WKUserContentController alloc] init]; // Do any additional setup after loading the view. } - (WKWebView *)webView{ if (!_webView) { WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init]; // config.backgroundColor = [UIColor clearColor]; // config.userContentController.backgroundColor = [UIColor clearColor]; // config.preferences.javaScriptEnabled = YES; WKUserContentController *userContentController = [[WKUserContentController alloc] init]; // 确保这里的self是实现了WKScriptMessageHandler协议的对象,并且名称与JavaScript中一致 [userContentController addScriptMessageHandler:self name:@"play_game"]; config.userContentController = userContentController; WKPreferences *preferences = [WKPreferences new]; preferences.javaScriptCanOpenWindowsAutomatically = YES; preferences.minimumFontSize = 0.0; preferences.javaScriptEnabled = YES; config.preferences = preferences; _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config]; _webView.opaque = NO; _webView.navigationDelegate = self; _webView.UIDelegate = self; _webView.scrollView.backgroundColor = [UIColor clearColor]; _webView.frame = self.view.bounds; [_webView setBackgroundColor:[UIColor clearColor]]; // UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // closeBtn.frame = CGRectMake(40, kStatusBarHeight, 30, 30); // [closeBtn setImage:[UIImage imageNamed:@"com_close_1"] forState:UIControlStateNormal]; // [closeBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside]; // [_webView addSubview:closeBtn]; // NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"BogoNetworkIndexModel"]; // BogoNetworkInitModel *model = [BogoNetworkInitModel mj_objectWithKeyValues:dict]; } return _webView; } - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { // 判断URL是否为"bogogame://exit",如果是,则隐藏webView if ([webView.URL.absoluteString isEqualToString:@"bogogame://exit"]) { [webView goBack]; } } - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSURL *url = navigationAction.request.URL; // 判断是否是您希望拦截的 URL,这里的示例是以 "myapp://" 开头的 URL if ([url.absoluteString isEqualToString:@"bogogame://exit"]) { [webView goBack]; // 自己处理逻辑,不加载该 URL decisionHandler(WKNavigationActionPolicyCancel); } else { // 允许加载该 URL game.dynasty168.com解析到哪里的 NSLog(@"web url %@",url.absoluteString) if ([url.absoluteString containsString:@"https://game.dynasty168.com"] && [url.absoluteString containsString:@"gameName"]){ GameViewController *vc = [[GameViewController alloc] init]; vc.url = url.absoluteString; [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES]; decisionHandler(WKNavigationActionPolicyCancel); return; } decisionHandler(WKNavigationActionPolicyAllow); } } - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{ if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust]; completionHandler(NSURLSessionAuthChallengeUseCredential,card); } } - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{ if ([message.name isEqualToString:@"play_game"]){ GameViewController *vc = [[GameViewController alloc] init]; // vc.url = NSLog(@"%@", [NSString stringWithFormat:@"%@",message.body]); [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES]; } } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end