一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - IOS - 詳解iOS webview加載時序和緩存問題總結(jié)

詳解iOS webview加載時序和緩存問題總結(jié)

2021-03-27 17:10lolDragon IOS

本篇文章主要介紹了iOS webview加載時序和緩存問題總結(jié) ,這兩天學(xué)習(xí)了Vue.js 感覺組件這個地方知識點(diǎn)挺多的,而且很重要,所以,今天添加一點(diǎn)小筆記。

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)簽:

 

復(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ò)請求不采用本地緩存和遠(yuǎn)程緩存

詳解iOS webview加載時序和緩存問題總結(jié)

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)于保存在沙盒中的緩存文件如下圖:

詳解iOS webview加載時序和緩存問題總結(jié)

詳解iOS webview加載時序和緩存問題總結(jié)

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产福利免费看 | 美国雪白人妖sarina | 乌克兰成人性色生活片 | 欧美生活一级片 | 亚洲精品综合网 | 欧美一区二区三区免费高 | 久久久久久久尹人综合网亚洲 | 国产成人免费片在线视频观看 | 插插好爽爽爽 | 91精品综合久久久久m3u8 | 亚洲高清成人 | 久草热在线 | 国产成+人+综合+欧美 亚洲 | 日韩人成 | 1769亚洲欧美资源站 | 亚洲欧洲网站 | 久久精品亚洲热综合一本 | 色综合欧美色综合七久久 | chinese国产打屁股 | 国产福利不卡视频在免费 | 欧美成人免费草草影院视频 | 日韩视频在线观看中字 | 色多多在线观看视频 | 亚洲精品久久久WWW游戏好玩 | 亚洲欧美日韩国产精品影院 | 倩女还魂在线观看完整版免费 | 91制片厂(果冻传媒)原档破解 | 色欧美亚洲 | 无人在线高清免费看 | 91麻豆精东果冻天美传媒老狼 | 午夜尤物| 日韩精品一区二区三区毛片 | 亚洲人的天堂男人爽爽爽 | 久久中文骚妇内射 | kuaibo成人播放器 | xxx88视频在线观看 | 日本不卡一区二区三区在线观看 | 天堂网在线网站成人午夜网站 | 国产大片视频免费观看 | 91麻豆精品国产自产在线观看 | a男人天堂 |