| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- //
- // GameViewController.m
- // BuguLive
- //
- // Created by Kylin on 2024/12/31.
- // Copyright © 2024 xfg. All rights reserved.
- //
- #import "GameViewController.h"
- @interface GameViewController()<WKNavigationDelegate>
- @property(nonatomic, strong) WKWebView *webView;
- @end
- @implementation GameViewController
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- if (@available(iOS 13, *)) {
- UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
- UIImage *image = [UIImage imageNamed:@"mine_navbg"];
- appearance.backgroundImage = image;
- self.navigationController.navigationBar.standardAppearance = appearance;
- self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
- } else {
- // 对于iOS 12及以下版本的兼容设置
- UINavigationBar *navigationBar = self.navigationController.navigationBar;
- UIImage *image = [UIImage imageNamed:@"mine_navbg"];
- [navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
- }
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- if (@available(iOS 13, *)) {
- UINavigationBarAppearance *defaultAppearance = [UINavigationBarAppearance new];
- self.navigationController.navigationBar.standardAppearance = defaultAppearance;
- self.navigationController.navigationBar.scrollEdgeAppearance = defaultAppearance;
- } else {
- // 对于iOS 12及以下版本的恢复设置
- UINavigationBar *navigationBar = self.navigationController.navigationBar;
- [navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- UIImageView *topImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -64, kScreenW, SCREEN_HEIGHT)];
- [topImgView setUserInteractionEnabled:YES];
- //
- // topImgView.image = [UIImage imageNamed:@"mainBg"];
- // topImgView.contentMode = UIViewContentModeScaleAspectFill;
- //// [self.view insertSubview:topImgView atIndex:0];
- [self.view addSubview:topImgView];
-
- [self.view addSubview:self.webView];
-
-
- [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
- [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.view);
- make.bottom.equalTo(self.view);
- make.left.equalTo(self.view);
- make.right.equalTo(self.view);
- }];
- [self.webView evaluateJavaScript:@"document.title" completionHandler:^(id title, NSError * _Nullable error) {
- self.title = title;
-
- }];
- // 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];
- _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
- _webView.opaque = NO;
- _webView.navigationDelegate = 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];
- [[AppDelegate sharedAppDelegate] popViewController];
- }
- if ([webView.URL.absoluteString containsString:@"https://game.dynasty168.com"]){
- [self setTitleStr];
- }
-
-
- }
- -(void)setTitleStr{
- [self.webView evaluateJavaScript:@"document.title" completionHandler:^(id title, NSError * _Nullable error) {
- self.title = title;
-
- }];
- }
- - (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];
- [[AppDelegate sharedAppDelegate] popViewController];
- // 自己处理逻辑,不加载该 URL
- decisionHandler(WKNavigationActionPolicyCancel);
- } else {
- // 允许加载该 URL
- NSLog(@"web url %@",url.absoluteString)
- decisionHandler(WKNavigationActionPolicyAllow);
- }
- }
- @end
- @interface CustomNavigationBarBackgroundView : UIView
- @property (nonatomic, strong) UIImageView *imageView;
- @end
- @implementation CustomNavigationBarBackgroundView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
- [self addSubview:self.imageView];
- }
- return self;
- }
- @end
|