AISleepCoachController.m 3.0 KB
//
//  AISleepCoachController.m
//  DreamSleep
//
//  Created by peter on 2022/4/1.
//

#import "AISleepCoachController.h"
#import <WebKit/WebKit.h>

@interface AISleepCoachController () <WKNavigationDelegate>
@property (strong, nonatomic) WKWebView *aiWebView;
@property (nonatomic, strong) UIProgressView *progressView;
@end

@implementation AISleepCoachController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建WKWebView对象,添加到界面(storyboard没有控件)
    [self.view addSubview:self.aiWebView];
    [self.view addSubview:self.progressView];
}

- (void)dealloc {
    [self.aiWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}

#pragma mark - WKWebView的监听方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.aiWebView.estimatedProgress;
        if (self.progressView.progress == 1) { self.progressView.hidden = YES; }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark - WKNavigationDelegate
// 开始加载
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

// 加载成功
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

// 加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    DSLog(@"百度主页加载失败:%@", error.userInfo);
}

- (WKWebView *)aiWebView {
    if (!_aiWebView) {
        _aiWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight)];
        _aiWebView.backgroundColor = DSWhite;
        
        [_aiWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
        _aiWebView.navigationDelegate = self;
        _aiWebView.scrollView.showsVerticalScrollIndicator = NO;
        _aiWebView.scrollView.showsHorizontalScrollIndicator = NO;
        
        // 测试地址:https://www.cbti.cn/sleep/ai/sleep_aicocah
        NSURL *url = [NSURL URLWithString:@"https://cbti.mynatapp.cc/sleep/ai/sleep_aicocah"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_aiWebView loadRequest:request];
    }
    return _aiWebView;
}

- (UIProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
        _progressView.tintColor = HighlightColor;
        // 通过放大Y轴来改变高度
        _progressView.transform = CGAffineTransformMakeScale(1.0f, 5.0f);;
    }
    return _progressView;
}

@end