使用UIWebView開發hybrid應用(二)

2021-07-09 11:45:18 字數 995 閱讀 7168

使用uiwebview開發hybrid應用(一)

hybrid應用中,快取處理是比較棘手的。

objective-c有快取的類nsurlcache,官方給出的解釋是:

it provides a composite in-memory and on-disk cache。也就是說,當應用關閉後,快取就失效了。為了使不經常變動的資料能持久化儲存,需要自己來實現。

思路:

uiwebview每請求乙個url,便將頁面及其對應資料儲存到本地。當使用者再次請求該資料時,直接載入disk裡的資料。

方案:

它們分為三類,以link、script、img開頭,很容易通過乙個正規表示式將它們提取出來。

nsregularexpression* regex = [nsregularexpression regularexpressionwithpattern:@"]+?(src|href)=[\"']?([^>'\"]+)[\"']?" options:nsregularexpressionallowcommentsandwhitespace error:nil];

nsarray* result = [regex matchesinstring:htmlstring options:nsmatchingreportcompletion range:nsmakerange(0, htmlstring.length)];

html檔案網路路徑替換成本地路徑。

2、再次請求該url時,uiwebview載入本地路徑,響應速度非常快。

[_webview loadhtmlstring:loadedstring baseurl:[nsurl fileurlwithpath:filepath]];
結語:

具體的方法在後面給出的demo中。當然,還有很多細節不能一一道出,但看看源**會清晰許多。程式也有需要優化地方,歡迎指出。

ios開發使用UIWebView顯示多種文件

使用uiwebview顯示多種文件 除了html的內容,uiwebview還可以顯示多種型別的文件 包括以下的型別 excel xls keynote key.zip numbers numbers.zip pages pages.zip pdf pdf powerpoint ppt word do...

ios開發使用UIWebView顯示多種文件

使用uiwebview顯示多種文件 除了html的內容,uiwebview還可以顯示多種型別的文件 包括以下的型別 excel xls keynote key.zip numbers numbers.zip pages pages.zip pdf pdf powerpoint ppt word do...

iOS開發 檢測專案使用UIWebView

前言 蘋果禁止使用uiwebview應用上架,這裡面包含兩層含義和兩個時間點 2020.4 和 2020.12 新應用的ipa 和 應用公升級ipa 2020年4月30日之後 也就是五月份之前 禁止新應用使用uiwebview 2020年12月30日之後,禁止所有提交的包使用uiwebview,也就...