WKWebView(IOS8+)
对比UIWebView(IOS2.0~12.0):
独立进程,内存;Crash不影响主App;对HTML和CSS更好的支持;更多更友好的系统函数;采用JIT技术。
使用WKWebView:
WebKit框架:
开源的Web浏览器引擎,对于IOS中的WebKit.framwork是在WebCore、底层桥接、JSCore引擎等核心模块等基础上,针对IOS平台的项目封装。
基本的加载:
通过configuration进行基本设置
基本的共享Cookie设置、基础偏好设置、播放视频设置、默认JS注入
加载URL&HTML
类比之前的UIKit提供的基础功能,在delegate中处理业务逻辑
-(instancetype)initWithFrame:(CGRect)frame
configuration:(WKWebViewConfiguration *)configuration;
-(nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
-(nullable WKNavigation *)loadHTMLString:(NSSTring *)string
baseURL:(nullable NSUL *)baseURL;
WKWebView Delegates
使用WebView加载简单的URL
//
// GSCDetailViewController.m
// GSCApp1
//
// Created by gsc on 2024/5/17.
//
#import "GSCDetailViewController.h"
#import "WebKit/WebKit.h"
@interface GSCDetailViewController ()<UIWebViewDelegate>
@property(nonatomic, strong, readwrite)WKWebView *webView;
@end
@implementation GSCDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:({
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 88, self.view.bounds.size.width, self.view.bounds.size.height - 88)];
self.webView;
})];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.youkuaiyun.com"]];
}
@end
使用WKWebView流程
1.创建WKWebView
2.设置Delegate以及样式、JS注入等
3.加载URL或HTML自付出啊
4.在相应的回调中处理业务逻辑
观察者模式KVO并展示页面加载进度
观察者模式
1.定义了一种一对多的关系,可以让多个观察者同时监听某一个对象或对象的属性变化
2.被监听的对象在状态变化时,会通知所有观察者,使他们能够及时的处理业务逻辑
//注册监听
/*
self作为监听者接受事件,监听self.webView的estimatedProgress属性,
在NSKeyValueObservingOptionNew的时候发通知
*/
[self.webView addObserver:self
forKeyPath:@"estimatedProgress"
options:NSKeyValueObservingOptionNew
context:nil];
//移除监听
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
//接收通知
-(void)observerValueForKeyPath:(nullable NSString *)keyPath
ofObject:(nullable id)object
change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change //change对应options
context:(nullable void *)context{
//业务逻辑
}
页面加载进度展示
//
// GSCDetailViewController.m
// GSCApp1
//
// Created by gsc on 2024/5/17.
//
#import "GSCDetailViewController.h"
#import "WebKit/WebKit.h"
@interface GSCDetailViewController ()<UIWebViewDelegate,WKNavigationDelegate>
@property(nonatomic, strong, readwrite)WKWebView *webView;
@property(nonatomic, strong, readwrite)UIProgressView *progressView;
@end
@implementation GSCDetailViewController
-(void)dealloc{
[self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:({
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 88, self.view.bounds.size.width, self.view.bounds.size.height - 88)];
self.webView.navigationDelegate = self;
self.webView;
})];
[self.view addSubview:({
self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 88, self.view.bounds.size.width, 20)];
self.progressView;
})];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.youkuaiyun.com"]]];
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context{
self.progressView.progress = self.webView.estimatedProgress;
NSLog(@"");
}
@end
系统KVO的问题:
1.需要移除Crash风险
2.复杂的方法名&参数