Cache the web file locally for faster access next time.
缓存web文件到本地以提高下次访问速度。
该库最低支持 iOS 9.0 和 Xcode 11。
- 在 Podfile 中添加 pod 'WebCacheFile'。
- 执行 pod install 或 pod update。
- 导入 <WebCacheFile/WebCacheFile.h>。
- 下载 WebCacheFile 文件夹内的所有内容。
- 将 WebCacheFile 内的源文件添加(拖放)到你的工程。
- 导入 WebCacheFile.h。
@implementation WkViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.注册需要拦截的协议类型
[WebCacheFile initRegister:@[@"http", @"https"]];
// 2.
[WebCacheFile setInterceptResourceTypes:@[@"js", @"css", @"png", @"jpg", @"gif"]];
// [self addWKWebView];
}
- (void)dealloc {
[WebCacheFile unregister];
}
@end
代码中已有Demo工程,可下载下来运行。示例截图: 第一次加载,会去下载。
注意,对于在WkWebView里 post 请求 body 数据被清空的临时处理方案时,在js层将body数据放入request header中,key固定为#define WkPostBodyKey @"headerBody"
。
Failed to execute 'setRequestHeader' on 'XMLHttpRequest': Value is not a valid ByteString
若提示上面信息,即请求的头信息中不能出现中文或UTF-8码的字符,需要js端使用encodeURIComponent
编码内容,iOS端使用- (NSString *)URLUTF8DecodingString
解码。
参考资料: