iOS中wkwebview加載本地html的要點


項目中有些頁面,我采用了html頁面開發,然后用wkwebview加載的設計。在加載過程中遇見了一些問題,在這里進行一些記載和討論。如有不同意見歡迎進行評論溝通。
問題時候這樣的:
在webview的loadrequest中不能加載出來
webview.m中的代碼:
//self.PageUrlString:加載的鏈接地址

 if (self.PageUrlString) {
         [self.wekView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.PageUrlString]]];
    }
獲取html的代碼:
  1. pathForResource
webVC.PageUrlString = [[NSBundle mainBundle] pathForResource:@"connectUs" ofType:@"html"]
結果:並不能正常加載,如下圖

pathForResource

經過研究,換用了獲取文件路徑的方法,就可以正常加載了

2.URLForResource

 webVC.fileUrl = [[NSBundle mainBundle] URLForResource:@"connectUs.html" withExtension:nil];

討論

1.pathForResource 和 2.URLForResource 獲取結果的區別

打印結果
image.png

結果中可以看出來,

1.pathForResource 出來的結果是純路徑
2.URLForResource 出來的加file協議的路徑

結論:

wkwebview 中loadrequest加載的url應該是是設置協議的路徑

調整:

  webVC.PageUrlString = [NSString stringWithFormat:@"file://%@",[[NSBundle mainBundle] pathForResource:@"connectUs" ofType:@"html"]];

  webVC.fileUrl = [[NSBundle mainBundle] URLForResource:@"connectUs.html" withExtension:nil];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM