




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】iOS如何解决webview加载时序和缓存问题
iOSwebView的加载时序-
(BOOL)webView:(UIWebView
*)webView
shouldStartLoadWithRequest:(NSURLRequest
*)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"开始请求webview:%@",request.URL.relativeString);
return
YES;
}
-
(void)webViewDidStartLoad:(UIWebView
*)webView
{
NSLog(@"开始加载webview");
}
-
(void)webViewDidFinishLoad:(UIWebView
*)webView
{
NSLog(@"结束加载webview");
}
-
(void)webView:(UIWebView
*)webView
didFailLoadWithError:(nonnull
NSError
*)error
{
NSLog(@"webView加载失败");
}
WKWebView加载时序:-
(void)webView:(WKWebView
*)webView
decidePolicyForNavigationAction:(WKNavigationAction
*)navigationAction
decisionHandler:(void
(^)(WKNavigationActionPolicy))decisionHandler
{
NSLog(@"webview开始请求");
decisionHandler(WKNavigationActionPolicyAllow);
}
-
(void)webView:(WKWebView
*)webView
didStartProvisionalNavigation:(WKNavigation
*)navigation
{
NSLog(@"webView开始加载");
}
-
(void)webView:(WKWebView
*)webView
decidePolicyForNavigationResponse:(WKNavigationResponse
*)navigationResponse
decisionHandler:(void
(^)(WKNavigationResponsePolicy))decisionHandler
{
NSLog(@"webview开始收到响应");
decisionHandler(WKNavigationResponsePolicyAllow);
}
显示开始加载html
CSS
js
和图片资源等(JS引擎单线程顺序执行)
-
(void)webView:(WKWebView
*)webView
didCommitNavigation:(WKNavigation
*)navigation
{
NSLog(@"1");
}
-
(void)webView:(WKWebView
*)webView
didFinishNavigation:(WKNavigation
*)navigation
{
NSLog(@"webview结束加载内容");
}
-
(void)webView:(WKWebView
*)webView
didFailProvisionalNavigation:(WKNavigation
*)navigation
withError:(NSError
*)error{
NSLog(@"webview加载失败");
}
-
(void)webView:(WKWebView
*)webView
didReceiveServerRedirectForProvisionalNavigation:(WKNavigation
*)navigation{
NSLog(@"开始重定向的函数");
}
-
(void)webView:(WKWebView
*)webView
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge
completionHandler:(void
(^)(NSURLSessionAuthChallengeDisposition,
NSURLCredential
*))completionHandler
{
NSLog(@"2");
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,
nil);
}iOSwebView加载html5缓存1.加载html5的过程2.设置清除html5页面缓存/upload/information/20200623/126/121217.png3.设置cssJS文件不从缓存中读取document.write(
"
<script
src='test.js?rnd=
"
+
Math.random()
+
"
'></s
"
+
"
cript>
"
)var
js
=
document.createElement(
"
script
"
)
js.src
=
"
test.js
"
+
Math.random()
document.body.appendChild(js)<script
src="test.js?ver=113"></script>4.iOS清除缓存文件-
(void)removeWebCache{
if
([[UIDevice
currentDevice].systemVersion
floatValue]
>=
9.0)
{
NSSet
*websiteDataTypes=
[NSSet
setWithArray:@[
WKWebsiteDataTypeDiskCache,
//WKWebsiteDataTypeOfflineWebApplication
WKWebsiteDataTypeMemoryCache,
//WKWebsiteDataTypeLocal
WKWebsiteDataTypeCookies,
//WKWebsiteDataTypeSessionStorage,
//WKWebsiteDataTypeIndexedDBDatabases,
//WKWebsiteDataTypeWebSQLDatabases
]];
//
All
kinds
of
data
//NSSet
*websiteDataTypes
=
[WKWebsiteDataStore
allWebsiteDataTypes];
NSDate
*dateFrom
=
[NSDate
dateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStore
defaultDataStore]
removeDataOfTypes:websiteDataTypes
modifiedSince:dateFrom
completionHandler:^{
}];
[[NSURLCache
sharedURLCache]
removeAllCachedResponses];
}
else
{
//先删除cookie
NSHTTPCookie
*cookie;
NSHTTPCookieStorage
*storage
=
[NSHTTPCookieStorage
sharedHTTPCookieStorage];
for
(cookie
in
[storage
cookies])
{
[storage
deleteCookie:cookie];
}
NSString
*libraryDir
=
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask,
YES)
objectAtIndex:0];
NSString
*bundleId
=
[[[NSBundle
mainBundle]
infoDictionary]
objectForKey:@"CFBundleIdentifier"];
NSString
*webkitFolderInLib
=
[NSString
stringWithFormat:@"%@/WebKit",libraryDir];
NSString
*webKitFolderInCaches
=
[NSString
stringWithFormat:@"%@/Caches/%@/WebKit",libraryDir,bundleId];
NSString
*webKitFolderInCachesfs
=
[NSString
stringWithFormat:@"%@/Caches/%@/fsCachedData",libraryDir,bundleId];
NSError
*error;
/*
iOS8.0
WebView
Cache的存放路径
*/
[[NSFileManager
defaultManager]
removeItemAtPath:webKitFolderInCaches
error:&error];
[[NSFileManager
defaultManager]
removeItemAtPath:webkitFolderInLib
error:nil];
/*
iOS7.0
WebView
Cache的存放路径
*/
[[NSFileManager
defaultManager]
removeItemAtPath:webKitFolderInCachesfs
error:&error];
NSString
*cookiesFolderPath
=
[libraryDir
stringByAppendingString:@"/Cookies"];
[[NSFileManager
defaultManager]
removeItemAtPath:cookiesFolderPath
error:&error];
[[NSURLCache
sharedURLCache]
removeAllCachedResponses];
}
}/upload/information/20200623/126/121218.png/upload/information/20200623/126/121219.png
-
(void)webViewDidFinishLoad:(UIWebView
*)webView
{
//防止内存泄漏
[[NSUserDefaults
standardUserDefaults]
setInteger:0
forKey:@"WebKitCacheModelPreferenceKey"];
//本地webkit硬盘图片的缓存;
[[NSUserDefaults
standardUserDefaults]
setBool:NO
forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的,原文没有提到。
//静止webkit离线缓存
[[NSUserDefaults
standardUserDefaults]
setBool:NO
forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的,,原文没有提到。
[[NSUserDefaults
standardUserDefaults]
synchronize];
}
-
(void)dealloc
{
[webView
loadHTMLString:@""
baseURL:nil];
[webView
stopLoading];
[webView
removeFromSuperview];
webView
=
nil;
[[NSURLCache
sharedURLCache]
removeAllCachedResponses];
[[NSURLCache
sharedURLCache]
setDiskCapacity:0];
[[NSURLCache
sharedURLCache]
setMemoryCapacity:0];
NSLog(@
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 纺织化学品应用拓展-洞察阐释
- 室内空间布局优化策略-洞察阐释
- 全球化背景下客户关系管理的分销创新-洞察阐释
- 【某商城消防系统的水力计算过程案例1000字】
- 香橙素对特应性皮炎小鼠的治疗作用
- 开放式基金业绩对基金流量影响的研究-基于有限理性视角下的分析
- 司机雇佣协议书10篇
- 土地的协议书合集15篇
- 车辆指标租赁与汽车保险代理服务协议
- 财务高管双重保密与竞业限制服务合同
- 优化港口装卸作业流程
- 联合办公协议书范本
- MBTI人格测试(自动生成结果)
- 40篇短文搞定高中英语3500单词
- 住院医师规范化培训教学活动指南(2021年版)六项指南全面解读合集
- 2023修正版《电力设施保护条例》
- 经皮冠状动脉介入治疗术术后健康饮食宣教
- 三体系内审检查表全条款
- 共生筑就康德莱30年发展
- 《健康饮食与健康》课件
- 工程项目移交交接单
评论
0/150
提交评论