AISleepCoachController.m
3.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// 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