没有合适的资源?快使用搜索试试~ 我知道了~
首页ios wkwebview离线化加载h5资源解决方案
资源详情
资源评论
资源推荐
ios wkwebview离线化加载离线化加载h5资源解决方案资源解决方案
本篇文章主要介绍了ios wkwebview离线化加载h5资源解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
思路: 使用NSURLProtocol拦截请求转发到本地。
1.确认离线化需求
部门负责的app有一部分使用的线上h5页,长期以来加载略慢...
于是考虑使用离线化加载。
确保[低速网络]或[无网络]可网页秒开。
2.使用[NSURLProtocol]拦截
区别于uiwebview wkwebview使用如下方法拦截
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 区别于uiwebview wkwebview使用如下方法拦截
Class cls = NSClassFromString(@"WKBrowsingContextController");
SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:");
if ([(id)cls respondsToSelector:sel]) {
[(id)cls performSelector:sel withObject:@"http"];
[(id)cls performSelector:sel withObject:@"https"];
}
}
# 注册NSURLProtocol拦截
- (IBAction)regist:(id)sender {
[NSURLProtocol registerClass:[FilteredProtocol class]];
}
# 注销NSURLProtocol拦截
- (IBAction)unregist:(id)sender {
[NSURLProtocol unregisterClass:[FilteredProtocol class]];
}
3.下载[zip] + 使用[SSZipArchive]解压
需要先 #import "SSZipArchive.h
- (void)downloadZip {
NSDictionary *_headers;
NSURLSession *_session = [self sessionWithHeaders:_headers];
NSURL *url = [NSURL URLWithString: @"http://10.2.138.225:3238/dist.zip"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 初始化cachepath
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSFileManager *fm = [NSFileManager defaultManager];
// 删除之前已有的文件
[fm removeItemAtPath:[cachePath stringByAppendingPathComponent:@"dist.zip"] error:nil];
NSURLSessionDownloadTask *downloadTask=[_session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
if (!error) {
NSError *saveError;
NSURL *saveUrl = [NSURL fileURLWithPath: [cachePath stringByAppendingPathComponent:@"dist.zip"]];
// location是下载后的临时保存路径,需要将它移动到需要保存的位置
[[NSFileManager defaultManager] copyItemAtURL:location toURL:saveUrl error:&saveError];
if (!saveError) {
NSLog(@"task ok");
if([SSZipArchive unzipFileAtPath:
[cachePath stringByAppendingPathComponent:@"dist.zip"]
toDestination:cachePath]) {
NSLog(@"unzip ok");// 解压成功
}
else {
NSLog(@"unzip err");// 解压失败
}
}
else {
NSLog(@"task err");
}
}
else {
NSLog(@"error is :%@", error.localizedDescription);
}
}];
[downloadTask resume];
}
4.迁移资源至[NSTemporary]
[wkwebview]真机不支持直接加载[NSCache]资源
需要先迁移资源至[NSTemporary]
- (void)migrateDistToTempory {
NSFileManager *fm = [NSFileManager defaultManager];
NSString *cacheFilePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"dist"];
NSString *tmpFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"dist"];
// 先删除tempory已有的dist资源
[fm removeItemAtPath:tmpFilePath error:nil];
NSError *saveError;
// 从caches拷贝dist到tempory临时文件夹
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:cacheFilePath] toURL:[NSURL fileURLWithPath:tmpFilePath] error:&saveError];
NSLog(@"Migrate dist to tempory ok");
}
5.转发请求
如果[/static]开头 => 则转发[Request]到本地[.css/.js]资源
如果[index.html]结尾 => 就直接[Load]本地[index.html] (否则[index.html]可能会加载失败)
//
// ProtocolCustom.m
// proxy-browser
weixin_38683848
- 粉丝: 4
- 资源: 950
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1