增加webview页面

This commit is contained in:
2025-11-10 16:09:47 +08:00
parent fac5e7657c
commit 2c8142c0d2
6 changed files with 296 additions and 40 deletions

View File

@@ -0,0 +1,177 @@
//
// KBWebViewViewController.m
// keyBoard
//
// Created by on 2025/11/10.
//
#import "KBWebViewViewController.h"
#import <WebKit/WebKit.h>
@interface KBWebViewViewController () <WKNavigationDelegate, WKScriptMessageHandler>
@property(nonatomic, strong) WKWebView * webView;
// 🟢
@property(nonatomic, strong) UIProgressView *progressView;
@property(nonatomic, assign) BOOL observingProgress;
@end
@implementation KBWebViewViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configUI];
}
- (void)configUI {
// 1. JS
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:self name:@"keyBoard"];
config.userContentController = userContentController;
// 2. webView
self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
self.webView.navigationDelegate = self;
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
self.webView.backgroundColor = UIColor.clearColor;
self.webView.frame = self.view.bounds;
[self.view addSubview:self.webView];
// 🟢 3. 2
self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
self.progressView.translatesAutoresizingMaskIntoConstraints = NO;
self.progressView.trackTintColor = [UIColor clearColor]; //
self.progressView.progressTintColor = [UIColor greenColor];
self.progressView.progress = 0.0f;
self.progressView.hidden = YES; //
[self.view addSubview:self.progressView];
// 2
[NSLayoutConstraint activateConstraints:@[
[self.progressView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
[self.progressView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[self.progressView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
[self.progressView.heightAnchor constraintEqualToConstant:2.0]
]];
// 🟢 4. WKWebView estimatedProgress
[self.webView addObserver:self
forKeyPath:@"estimatedProgress"
options:NSKeyValueObservingOptionNew
context:nil];
self.observingProgress = YES;
// 5. URL
NSURLRequest * req = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
[self.webView loadRequest:req];
}
#pragma mark - KVO: estimatedProgress 🟢
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
if (object == self.webView && [keyPath isEqualToString:@"estimatedProgress"]) {
CGFloat progress = self.webView.estimatedProgress;
// 0 ~ 1
self.progressView.hidden = NO;
[self.progressView setProgress:progress animated:YES];
if (progress >= 1.0f) {
//
[UIView animateWithDuration:0.25
delay:0.25
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.progressView.alpha = 0.0;
} completion:^(BOOL finished) {
self.progressView.progress = 0.0;
self.progressView.hidden = YES;
self.progressView.alpha = 1.0;
}];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark - WKNavigationDelegate
//
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
NSLog(@"开始加载url");
//
self.progressView.hidden = NO;
self.progressView.progress = 0.0;
}
//
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"页面加载成功");
self.title = webView.title;
}
//
- (void)webView:(WKWebView *)webView
didFailProvisionalNavigation:(WKNavigation *)navigation
withError:(NSError *)error {
NSLog(@"webView load error: %@", error);
//
self.progressView.hidden = YES;
self.progressView.progress = 0.0;
}
#pragma mark - WKScriptMessageHandler JS
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
if (![message.body isKindOfClass:NSDictionary.class]) {
NSLog(@"Body参数不合法");
return;
}
[self handleJSMethodWithParams:message.body];
}
- (void)handleJSMethodWithParams:(NSDictionary *)params {
NSDictionary * body = [params copy];
NSLog(@"收到 JS 消息: %@", body);
NSString * type = body[@"type"];
//
if ([type isEqual:@"ONE_METHOD"]) {
NSLog(@"come on baby js 调用你的方法了");
}
// title
if ([type isEqual:@"changeTiele"]) {
NSLog(@"%@", body);
NSString * newTiele = body[@"payload"][@"data"];
self.title = newTiele;
}
}
#pragma mark - Clean up 🟢
- (void)dealloc {
if (self.observingProgress) {
@try {
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
} @catch (NSException *exception) {
NSLog(@"removeObserver estimatedProgress exception: %@", exception);
}
}
// 便 JS
[self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"keyBoard"];
}
@end