ios webview的加載時序
uiwebview加載順序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
- ( 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加載時序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
- ( 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、字體文件、圖片都是一個獨(dú)立的http請求,每一個請求都串行的(可能有連接復(fù)用)。
2.設(shè)置清除html5頁面緩存
html5端設(shè)置meta標(biāo)簽:
<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ò)請求不采用本地緩存和遠(yuǎn)程緩存
ps:設(shè)置上面的只是緊緊可以保證html文件每次從服務(wù)器中獲取,不從緩存文件中拿,而對于外聯(lián)css js圖片等文件仍舊是從緩存中獲取的;
3.設(shè)置css js文件不從緩存中讀取
通過添加版本號的和隨機(jī)數(shù)的方法,保證每次加載js css連接都是最新的,通常的做法是添加一個版本號,在每次更新了js css時給版本號+1;保證沒有更新時采用緩存文件
有更新可以從服務(wù)中獲取;
解決方法
1、隨機(jī)數(shù)法
方法一:
1
|
document.write( " <script src='test.js?rnd= " + math.random() + " '></s " + " cript> " ) |
方法二:
1
2
3
4
5
|
var js = document.createelement( " script " ) js.src = " test.js " + math.random() document.body.appendchild(js) |
這樣采用隨機(jī)數(shù)的話, js文件將永遠(yuǎn)得不到緩存,每次都必須重新從服務(wù)器加載,即使沒有任何更改。
大家如果經(jīng)常上國外網(wǎng)站的話,可以看到他們通常采用這樣的方式來解決:
1
|
<script src= "test.js?ver=113" ></script> |
其中 ver=113 的 113就是版本號
這樣真正做到了應(yīng)該緩存的時候緩存靜態(tài)文件,當(dāng)版本有更新的時候從獲取最新的版本,并更新緩存。
對于圖像 <img src="test.jps?ver=版本號"> 來有效利用和更新緩存.
4.ios清除緩存文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
- ( 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)上)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
- ( 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后重新進(jìn)入,第一次加載webview,都會加載全部的數(shù)據(jù)資源(外聯(lián)js,外聯(lián)css,圖片等)退出去后,如果在沒有更新js,css內(nèi)容時,默認(rèn)只會加載html內(nèi)容,ps:html中的內(nèi)容 在每次加載webview中都會從服務(wù)器中更新一下;
2.如果js css后面都添加了版本號,那么在每次更新版本號時,或者說資源鏈接變化時,webview一定會重新加載新的內(nèi)容;如下圖
1
|
<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)?
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/lolDragon/p/6774509.html