這篇文章將為大家詳細講解有關(guān)iOS如何解決webview加載時序和緩存問題,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供鞏義網(wǎng)站建設(shè)、鞏義做網(wǎng)站、鞏義網(wǎng)站設(shè)計、鞏義網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、鞏義企業(yè)網(wǎng)站模板建站服務(wù),10多年鞏義做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
iOS webView的加載時序
UIWebView加載順序:
- (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(@"結(jié)束加載webview"); } - (void)webView:(UIWebView *)webView didFailLoadWithError:(nonnull NSError *)error { NSLog(@"webView加載失敗"); }
加載的結(jié)果:
2017-04-27 08:53:00.535 H5頁面調(diào)試[1273:150877] 開始請求webview:http://xxxx/index1.html
2017-04-27 08:53:00.537 H5頁面調(diào)試[1273:150877] 開始加載webview
-----------------顯示開始加載html CSS js 和圖片資源等(JS引擎單線程順序執(zhí)行)---------------
2017-04-27 08:53:01.069 H5頁面調(diào)試[1273:150877] 結(jié)束加載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開始收到響應(yīng)"); decisionHandler(WKNavigationResponsePolicyAllow); } -----------------顯示開始加載html CSS js 和圖片資源等(JS引擎單線程順序執(zhí)行)--------------- - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation { NSLog(@"1"); } - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { NSLog(@"webview結(jié)束加載內(nèi)容"); } - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{ NSLog(@"webview加載失敗"); } - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{ NSLog(@"開始重定向的函數(shù)"); } - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler { NSLog(@"2"); completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); }
iOS webView加載html5緩存
1.加載html5的過程
每次加載一個HTML5頁面,都會有較多的請求。除了HTML主URL自身的請求外,HTML外部引用的JS、CSS、字體文件、圖片都是一個獨立的HTTP請求,每一個請求都串行的(可能有連接復(fù)用)。
2.設(shè)置清除html5頁面緩存
html5端設(shè)置meta標(biāo)簽:
復(fù)制代碼 代碼如下:
<meta http-equiv="Pragma" content="no-cache" /><meta http-equiv="Cache-Control" content="no-cache" /><meta http-equiv="Expires" content="0" />
ios:設(shè)置加載的網(wǎng)絡(luò)請求不采用本地緩存和遠程緩存
PS:設(shè)置上面的只是緊緊可以保證html文件每次從服務(wù)器中獲取,不從緩存文件中拿,而對于外聯(lián)CSS JS圖片等文件仍舊是從緩存中獲取的;
3.設(shè)置css JS文件不從緩存中讀取
通過添加版本號的和隨機數(shù)的方法,保證每次加載JS CSS連接都是最新的,通常的做法是添加一個版本號,在每次更新了JS CSS時給版本號+1;保證沒有更新時采用緩存文件
有更新可以從服務(wù)中獲取;
解決方法
1、隨機數(shù)法
方法一:
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)
這樣采用隨機數(shù)的話, js文件將永遠得不到緩存,每次都必須重新從服務(wù)器加載,即使沒有任何更改。
大家如果經(jīng)常上國外網(wǎng)站的話,可以看到他們通常采用這樣的方式來解決:
<script src="test.js?ver=113"></script>
其中 ver=113 的 113就是版本號
這樣真正做到了應(yīng)該緩存的時候緩存靜態(tài)文件,當(dāng)版本有更新的時候從獲取最新的版本,并更新緩存。
對于圖像 <img src="test.jps?ver=版本號"> 來有效利用和更新緩存.
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]; } }
關(guān)于保存在沙盒中的緩存文件如下圖:
5.針對UIWebView出現(xiàn)的內(nèi)存泄漏方法(網(wǎng)上)
- (void)webViewDidFinishLoad:(UIWebView *)webView { //防止內(nèi)存泄漏 [[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(@"釋放了webview"); } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ int cacheSizeMemory = 4*1024*1024; // 4MB int cacheSizeDisk = 32*1024*1024; // 32MB NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"]; [NSURLCache setSharedURLCache:sharedCache]; } - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[NSURLCache sharedURLCache] removeAllCachedResponses]; }
PS:經(jīng)測試好像沒什么卵用,先放在這里反正寫了也沒什么壞處
確定
1.如果沒有cdn緩存影響;每次殺死APP后重新進入,第一次加載webview,都會加載全部的數(shù)據(jù)資源(外聯(lián)js,外聯(lián)css,圖片等)退出去后,如果在沒有更新js,css內(nèi)容時,默認只會加載html內(nèi)容,PS:html中的內(nèi)容 在每次加載webView中都會從服務(wù)器中更新一下;
2.如果js css后面都添加了版本號,那么在每次更新版本號時,或者說資源鏈接變化時,webView一定會重新加載新的內(nèi)容;如下圖
<script type="text/javascript" src="index1.js?v=1.0.0"></script>
疑問?
1.經(jīng)測試發(fā)現(xiàn),JS CSS沒有加版本號,更新JS CSS的內(nèi)容有時候也會及時從服務(wù)器中更新獲取,大多數(shù)時候又不會更新;不知道是不是跟web服務(wù)器的緩存策略有關(guān),還是文件超期了?還是CDN緩存有關(guān)?
關(guān)于“iOS如何解決webview加載時序和緩存問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章名稱:iOS如何解決webview加載時序和緩存問題
標(biāo)題來源:http://muchs.cn/article44/jpeihe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計、網(wǎng)站營銷、網(wǎng)站改版、定制開發(fā)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)