GameViewController.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // GameViewController.m
  3. // BuguLive
  4. //
  5. // Created by Kylin on 2024/12/31.
  6. // Copyright © 2024 xfg. All rights reserved.
  7. //
  8. #import "GameViewController.h"
  9. @interface GameViewController()<WKNavigationDelegate>
  10. @property(nonatomic, strong) WKWebView *webView;
  11. @end
  12. @implementation GameViewController
  13. - (void)viewWillAppear:(BOOL)animated {
  14. [super viewWillAppear:animated];
  15. if (@available(iOS 13, *)) {
  16. UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  17. UIImage *image = [UIImage imageNamed:@"mine_navbg"];
  18. appearance.backgroundImage = image;
  19. self.navigationController.navigationBar.standardAppearance = appearance;
  20. self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
  21. } else {
  22. // 对于iOS 12及以下版本的兼容设置
  23. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  24. UIImage *image = [UIImage imageNamed:@"mine_navbg"];
  25. [navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
  26. }
  27. }
  28. - (void)viewWillDisappear:(BOOL)animated {
  29. [super viewWillDisappear:animated];
  30. if (@available(iOS 13, *)) {
  31. UINavigationBarAppearance *defaultAppearance = [UINavigationBarAppearance new];
  32. self.navigationController.navigationBar.standardAppearance = defaultAppearance;
  33. self.navigationController.navigationBar.scrollEdgeAppearance = defaultAppearance;
  34. } else {
  35. // 对于iOS 12及以下版本的恢复设置
  36. UINavigationBar *navigationBar = self.navigationController.navigationBar;
  37. [navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  38. }
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. UIImageView *topImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -64, kScreenW, SCREEN_HEIGHT)];
  43. [topImgView setUserInteractionEnabled:YES];
  44. //
  45. // topImgView.image = [UIImage imageNamed:@"mainBg"];
  46. // topImgView.contentMode = UIViewContentModeScaleAspectFill;
  47. //// [self.view insertSubview:topImgView atIndex:0];
  48. [self.view addSubview:topImgView];
  49. [self.view addSubview:self.webView];
  50. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
  51. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.top.equalTo(self.view);
  53. make.bottom.equalTo(self.view);
  54. make.left.equalTo(self.view);
  55. make.right.equalTo(self.view);
  56. }];
  57. [self.webView evaluateJavaScript:@"document.title" completionHandler:^(id title, NSError * _Nullable error) {
  58. self.title = title;
  59. }];
  60. // Do any additional setup after loading the view.
  61. }
  62. - (WKWebView *)webView{
  63. if (!_webView) {
  64. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  65. // config.backgroundColor = [UIColor clearColor];
  66. // config.userContentController.backgroundColor = [UIColor clearColor];
  67. _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
  68. _webView.opaque = NO;
  69. _webView.navigationDelegate = self;
  70. _webView.scrollView.backgroundColor = [UIColor clearColor];
  71. _webView.frame = self.view.bounds;
  72. [_webView setBackgroundColor:[UIColor clearColor]];
  73. // UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  74. // closeBtn.frame = CGRectMake(40, kStatusBarHeight, 30, 30);
  75. // [closeBtn setImage:[UIImage imageNamed:@"com_close_1"] forState:UIControlStateNormal];
  76. // [closeBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
  77. // [_webView addSubview:closeBtn];
  78. // NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"BogoNetworkIndexModel"];
  79. // BogoNetworkInitModel *model = [BogoNetworkInitModel mj_objectWithKeyValues:dict];
  80. }
  81. return _webView;
  82. }
  83. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  84. // 判断URL是否为"bogogame://exit",如果是,则隐藏webView
  85. if ([webView.URL.absoluteString isEqualToString:@"bogogame://exit"]) {
  86. // [webView goBack];
  87. [[AppDelegate sharedAppDelegate] popViewController];
  88. }
  89. if ([webView.URL.absoluteString containsString:@"https://game.dynasty168.com"]){
  90. [self setTitleStr];
  91. }
  92. }
  93. -(void)setTitleStr{
  94. [self.webView evaluateJavaScript:@"document.title" completionHandler:^(id title, NSError * _Nullable error) {
  95. self.title = title;
  96. }];
  97. }
  98. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
  99. decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  100. NSURL *url = navigationAction.request.URL;
  101. // 判断是否是您希望拦截的 URL,这里的示例是以 "myapp://" 开头的 URL
  102. if ([url.absoluteString isEqualToString:@"bogogame://exit"]) {
  103. // [webView goBack];
  104. [[AppDelegate sharedAppDelegate] popViewController];
  105. // 自己处理逻辑,不加载该 URL
  106. decisionHandler(WKNavigationActionPolicyCancel);
  107. } else {
  108. // 允许加载该 URL
  109. NSLog(@"web url %@",url.absoluteString)
  110. decisionHandler(WKNavigationActionPolicyAllow);
  111. }
  112. }
  113. @end
  114. @interface CustomNavigationBarBackgroundView : UIView
  115. @property (nonatomic, strong) UIImageView *imageView;
  116. @end
  117. @implementation CustomNavigationBarBackgroundView
  118. - (instancetype)initWithFrame:(CGRect)frame {
  119. self = [super initWithFrame:frame];
  120. if (self) {
  121. self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
  122. [self addSubview:self.imageView];
  123. }
  124. return self;
  125. }
  126. @end