GameListViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // GameListViewController.m
  3. // BuguLive
  4. //
  5. // Created by voidcat on 2023/8/29.
  6. // Copyright © 2023 xfg. All rights reserved.
  7. //
  8. #import "GameListViewController.h"
  9. #import "GameViewController.h"
  10. @interface GameListViewController ()<WKNavigationDelegate,WKScriptMessageHandler,WKUIDelegate>
  11. //webview
  12. @property(nonatomic, strong) WKWebView *webView;
  13. @property (nonatomic, strong) WKUserContentController *userContentC;
  14. @end
  15. @implementation GameListViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. [self.view addSubview:self.webView];
  19. NSString *url = [GlobalVariables sharedInstance].appModel.h5_url.game_list;
  20. url = [url urlAddCompnentForValue:[IMAPlatform sharedInstance].host.userId key:@"uid"];
  21. url = [url urlAddCompnentForValue:[BogoNetwork shareInstance].token key:@"token"];
  22. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  23. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  24. make.top.equalTo(self.view).offset(20);
  25. make.bottom.equalTo(self.view).offset(-20);
  26. make.left.equalTo(self.view);
  27. make.right.equalTo(self.view);
  28. }];
  29. // self.userContentC = [[WKUserContentController alloc] init];
  30. // Do any additional setup after loading the view.
  31. }
  32. - (WKWebView *)webView{
  33. if (!_webView) {
  34. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  35. // config.backgroundColor = [UIColor clearColor];
  36. // config.userContentController.backgroundColor = [UIColor clearColor];
  37. // config.preferences.javaScriptEnabled = YES;
  38. WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  39. // 确保这里的self是实现了WKScriptMessageHandler协议的对象,并且名称与JavaScript中一致
  40. [userContentController addScriptMessageHandler:self name:@"play_game"];
  41. config.userContentController = userContentController;
  42. WKPreferences *preferences = [WKPreferences new];
  43. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  44. preferences.minimumFontSize = 0.0;
  45. preferences.javaScriptEnabled = YES;
  46. config.preferences = preferences;
  47. _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
  48. _webView.opaque = NO;
  49. _webView.navigationDelegate = self;
  50. _webView.UIDelegate = self;
  51. _webView.scrollView.backgroundColor = [UIColor clearColor];
  52. _webView.frame = self.view.bounds;
  53. [_webView setBackgroundColor:[UIColor clearColor]];
  54. // UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  55. // closeBtn.frame = CGRectMake(40, kStatusBarHeight, 30, 30);
  56. // [closeBtn setImage:[UIImage imageNamed:@"com_close_1"] forState:UIControlStateNormal];
  57. // [closeBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
  58. // [_webView addSubview:closeBtn];
  59. // NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"BogoNetworkIndexModel"];
  60. // BogoNetworkInitModel *model = [BogoNetworkInitModel mj_objectWithKeyValues:dict];
  61. }
  62. return _webView;
  63. }
  64. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  65. // 判断URL是否为"bogogame://exit",如果是,则隐藏webView
  66. if ([webView.URL.absoluteString isEqualToString:@"bogogame://exit"]) {
  67. [webView goBack];
  68. }
  69. }
  70. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
  71. decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  72. NSURL *url = navigationAction.request.URL;
  73. // 判断是否是您希望拦截的 URL,这里的示例是以 "myapp://" 开头的 URL
  74. if ([url.absoluteString isEqualToString:@"bogogame://exit"]) {
  75. [webView goBack];
  76. // 自己处理逻辑,不加载该 URL
  77. decisionHandler(WKNavigationActionPolicyCancel);
  78. } else {
  79. // 允许加载该 URL game.dynasty168.com解析到哪里的
  80. NSLog(@"web url %@",url.absoluteString)
  81. if ([url.absoluteString containsString:@"https://game.dynasty168.com"] && [url.absoluteString containsString:@"gameName"]){
  82. GameViewController *vc = [[GameViewController alloc] init];
  83. vc.url = url.absoluteString;
  84. [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES];
  85. decisionHandler(WKNavigationActionPolicyCancel);
  86. return;
  87. }
  88. decisionHandler(WKNavigationActionPolicyAllow);
  89. }
  90. }
  91. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
  92. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
  93. NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
  94. completionHandler(NSURLSessionAuthChallengeUseCredential,card);
  95. }
  96. }
  97. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
  98. if ([message.name isEqualToString:@"play_game"]){
  99. GameViewController *vc = [[GameViewController alloc] init];
  100. // vc.url =
  101. NSLog(@"%@", [NSString stringWithFormat:@"%@",message.body]);
  102. [[AppDelegate sharedAppDelegate] pushViewController:vc animated:YES];
  103. }
  104. }
  105. /*
  106. #pragma mark - Navigation
  107. // In a storyboard-based application, you will often want to do a little preparation before navigation
  108. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  109. // Get the new view controller using [segue destinationViewController].
  110. // Pass the selected object to the new view controller.
  111. }
  112. */
  113. @end