ios WKWebView详解
来源:互联网 发布:淘宝网高档雪纺练功服 编辑:程序博客网 时间:2024/05/29 05:03
项目中,使用到web加载,是件再正常不过的事情.之前一直使用UIWebView.但ios8后,苹果推出了WKWebView.性能优化更加完善.并以更加稳定,加载滑动等功能方面更加流畅,内存占用也相对少而深受喜欢!今天详细扒一下WKWebView在项目中的使用.包含加载进度条设置,获取web界面标题,屏幕自适应,web界面是否允许跳转等功能实现!
直接粘代码:
.h文件
@interface WebVC :UIViewController
//添加外接属性,获取跳转url
@property(nonatomic,copy)NSString * urlStr;
@end
.m文件
//wkWebView
#import <WebKit/WebKit.h>
#import "WebVC.h"
//实现wk的协议方法,及滑动scrollView的协议方法
@interface WebVC () <WKNavigationDelegate,UIScrollViewDelegate>
@property (nonatomic,strong) WKWebView *webView;
@property(nonatomic,strong)UIProgressView * progressView;
@end
@implementation JKWebVC
-(void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
//加载url
[self.webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:self.urlStr]]];
//添加kvo监听,获得页面title和加载进度值
[self.webViewaddObserver:selfforKeyPath:@"estimatedProgress"options:NSKeyValueObservingOptionNewcontext:NULL];
[self.webViewaddObserver:selfforKeyPath:@"title"options:NSKeyValueObservingOptionNewcontext:NULL];
}
//添加KVO监听
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
//加载进度值
if ([keyPathisEqualToString:@"estimatedProgress"])
{
if (object ==self.webView)
{
[self.progressViewsetAlpha:1.0f];
[self.progressViewsetProgress:self.webView.estimatedProgressanimated:YES];
if(self.webView.estimatedProgress >= 1.0f)
{
[UIViewanimateWithDuration:0.5f
delay:0.3f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.progressViewsetAlpha:0.0f];
}
completion:^(BOOL finished) {
[self.progressViewsetProgress:0.0f animated:NO];
}];
}
}
else
{
[superobserveValueForKeyPath:keyPathofObject:object change:changecontext:context];
}
}
//网页title
else if ([keyPath isEqualToString:@"title"])
{
if (object ==self.webView)
{
self.navigationItem.title =self.webView.title;
}
else
{
[superobserveValueForKeyPath:keyPathofObject:object change:changecontext:context];
}
}
else
{
[superobserveValueForKeyPath:keyPathofObject:object change:changecontext:context];
}
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
self.progressView.hidden =NO;
self.progressView.transform =CGAffineTransformMakeScale(1.0f, 1.5f);
[self.viewbringSubviewToFront:self.progressView];
}
// WKNavigationDelegate 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
//加载完成后隐藏progressView
self.progressView.hidden =YES;
//修改字体大小 300%
[ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'"completionHandler:nil];
//修改字体颜色 #9098b8
[webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#222222'"completionHandler:nil];
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
//加载失败同样需要隐藏progressView
self.progressView.hidden =YES;
[self.webViewremoveFromSuperview];
[self.viewaddSubview:self.blankPageView];
}
//点击跳转新界面
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler
{
NSString * strRequest =[navigationAction.request.URL.absoluteStringstringByRemovingPercentEncoding];
if([strRequestisEqualToString:@"about:blank"]) {//主页面加载内容
decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
} else {//截获页面里面的链接点击
decisionHandler(WKNavigationActionPolicyAllow);//跳转
}
}
#pragma mark --------- 懒加载 --------------
- (void)viewDidLoad {
[superviewDidLoad];
}
- (WKWebView *)webView
{
if (!_webView) {
WKWebViewConfiguration *config = [[WKWebViewConfigurationalloc]init];
//初始化偏好设置属性:preferences
config.preferences = [[WKPreferencesalloc]init];
//The minimum font size in points default is 0;
config.preferences.minimumFontSize = 0;
//是否支持JavaScript
config.preferences.javaScriptEnabled =YES;
//不通过用户交互,是否可以打开窗口
config.preferences.javaScriptCanOpenWindowsAutomatically =YES;
//适应屏幕
NSString *jSString =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUserScript = [[WKUserScriptalloc] initWithSource:jSStringinjectionTime:WKUserScriptInjectionTimeAtDocumentEndforMainFrameOnly:YES];
WKUserContentController * wkUController =[[WKUserContentControlleralloc]init];
[wkUController addUserScript:wkUserScript];
config .userContentController =wkUController;
_webView = [[WKWebViewalloc]initWithFrame:CGRectMake(0, 0,JkScreenWidth,JkScreenHeight)configuration:config];
_webView.scrollView.delegate =self;
_webView.backgroundColor = [UIColorwhiteColor];
_webView.scrollView.showsVerticalScrollIndicator =NO;
[self.viewaddSubview:_webView];
}
return_webView;
}
-(UIProgressView *)progressView
{
if (!_progressView) {
_progressView =[[UIProgressViewalloc] initWithFrame:CGRectMake(0, 64, [[UIScreenmainScreen] bounds].size.width, 1)];
_progressView.backgroundColor = [UIColorwhiteColor];
_progressView.progressTintColor= [UIColorredColor];//设置已过进度部分的颜色
_progressView.trackTintColor= [UIColorlightGrayColor];//设置未过进度部分的颜色
//设置进度条的高度,下面这句代码表示进度条的宽度变为原来的1倍,高度变为原来的1.5倍.
self.progressView.transform =CGAffineTransformMakeScale(1.0f, 1.5f);
[self.viewaddSubview:_progressView];
}
return_progressView;
}
//移除监听者
- (void)dealloc {
[self.webViewremoveObserver:selfforKeyPath:@"estimatedProgress"];
[self.webViewremoveObserver:selfforKeyPath:@"title"];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- iOS WKWebView 详解
- ios WKWebView详解
- IOS WKWebView
- 【iOS】WKWebView
- iOS - WKWebView
- IOS-WKWebView
- iOS网络3—UIWebView与WKWebView使用详解
- iOS UIWebView和WKWebView的JS调用详解
- iOS-WKWebView使用
- IOS 进阶之 WKWebView
- iOS WKWebView的使用
- iOS开发 之 WKWebView
- iOS Objective-C WKWebView
- iOS WKWebView js交互
- ##iOS 8新增 WKWebView
- iOS-WKWebView的封装
- iOS wkwebview 弹出键盘
- ios 清 wkwebview 缓存
- spring boot注解
- three.js 03-02 之 PointLight 光源
- python 计算积分图和haar特征
- 【转载】从淘宝购物分析背后的技术
- 1024. Palindromic Number (25)
- ios WKWebView详解
- Pioneer3 声呐扫描顺序?
- C++数值类型与string的相互转换
- integral channel features (通俗易懂版)
- 测试一下
- 单例的七种写法以及一些排序算法
- Java I/O系统之转换流
- 存储两次序列化对象,读取报错问题 java.io.StreamCorruptedException: invalid type code: A
- javascript之全选、反选和不选