IOS开发学习日记(五)

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.csdn.net"]];
    
    
}

@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.csdn.net"]]];
    [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.复杂的方法名&参数

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我真的学不会了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值