IOS wkwebview caching and real-time


When it comes to the caching of WKWebView, I've also written an article about the caching of UIWebView. The address of the article is as follows:

IOS uiwebview caching and real time

But this method lost its effect on WKWebView. Later, all the company's projects were changed to WKWebView. As for the benefits of WKWebView, this is just a little more. If the above method fails on WKWebView, then I also modified the new scheme, and I have been maintaining several of my own software in my spare time ZFJObsLib,ZFJPyMix , so I haven't had time to summarize and optimize. Here I have re optimized zfjcache protocol. You can also optimize your design according to your own understanding and needs. I cache all the data in UIWebView, where developers can choose the type of cache by themselves!

The specific flow chart is as follows:

If you want to know more about the caching mechanism of NSURLRequest, you can see IOS uiwebview caching and real time

The detailed API is as follows:


Usage effect

Using tutorials

Just turn on network monitoring on the page you want to monitor, and cancel monitoring when you don't need to monitor, as follows:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //Start listening to cached data
    [ZFJCacheProtocol startMonitorRequest:ZFJCacheAllType];

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //Cancel monitoring even when webview is not needed
    [ZFJCacheProtocol cancelMonitorRequest];

Custom configuration

1. Custom cache type

ZFJWebCache can distinguish the data types of the cache according to the MIME type. The specific cache types are as follows:

typedef NS_ENUM(NSInteger, ZFJCacheType) {
    ZFJCacheAllType         = 1 << 0,     //Cache all
    ZFJCacheImageType       = 1 << 1,     //Cache images
    ZFJCacheVideoType       = 1 << 2,     //Cache video
    ZFJCacheAudioType       = 1 << 3,     //Cache audio
    ZFJCacheTextType        = 1 << 4,     //Cache text
    ZFJCacheMessageType     = 1 << 5,     //Cache message
    ZFJCacheX_WorldType     = 1 << 6,     //Cache x-world
    ZFJCacheApplicationType = 1 << 7,     //Cache application
    ZFJCacheNoneType        = 1 << 8,     //unknown type

For example, you only want to cache image as follows:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //Start listening to cached data
    [ZFJCacheProtocol startMonitorRequest:ZFJCacheImageType];

2. Set cache expiration time

The default expiration time of ZFJWebCache is one hour (3600s). If you want to customize the cache expiration time, you can set it as follows:

// Set cache expiration time
    [ZFJCacheConfig instance].updateInterval = 1200;

3. Set ignore url

NSArray *ignoreUrlArray = @[@""];
[ZFJCacheConfig instance].ignoreUrlArray = ignoreUrlArray;

4. Other configuration API s are as follows

//  ZFJCacheConfig.h
//  Ahaschool
//  Created by ZFJMBPro on 2020/1/13.
//  Copyright © 2020 ahaschool. All rights reserved.

#import <Foundation/Foundation.h>
#import "ZFJMIMEType.h"

@interface ZFJCacheConfig : NSObject

+ (instancetype)instance;

///Last url request time
@property (readwrite, nonatomic, strong) NSDictionary *lastRequestDict;

///Supported cache types
@property (nonatomic,assign) ZFJCacheType cacheType;

///Same Url request cache interval (3600s by default)
@property (readwrite, nonatomic, assign) NSInteger updateInterval;

///Configure the policies required for some requests for NSURLSession
@property (readwrite, nonatomic, strong) NSURLSessionConfiguration *config;

///Request task queue
@property (readwrite, nonatomic, strong) NSOperationQueue *forgeroundNetQueue;

///Cache task queue
@property (readwrite, nonatomic, strong) NSOperationQueue *backgroundNetQueue;

///Ignored url links
@property (nonatomic, strong) NSArray *ignoreUrlArray;

///Clear the url record in memory (you can call this method to clear the url record in memory when receiving the memory warning)
- (void)clearUrlDict;

///Get cache path
- (NSString *)documentPath;

///Whether cache is supported
- (BOOL)isSupportCache:(NSString *)mimeType;


Source code


Concluding remarks

Welcome everyone to provide valuable suggestions and opinions, also welcome everyone to join the group or add my QQ to exchange and learn!


154 original articles published, 380 praised, 620000 visitors+
His message board follow

Tags: iOS network

Posted on Thu, 16 Jan 2020 05:38:31 -0800 by IceRegent