關於 UIWebView 載入後為空白頁
阿新 • • 發佈:2019-01-02
UIWebView 載入後為空白頁
在使用 UIWebView的loadRequest 之後頁面顯示為空白頁,原因是很簡單,其實可以使用的 UIWebView的 delegate方法來輸出它的錯誤原因.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error;
引數error 就是WebView 的提示錯誤.一般情況都是 url不正確導致的,可以用瀏覽器試試 URL是否正確,不正確那麼就可能是 url含有特殊字元,沒有將 url轉碼之前是使用
- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc NS_DEPRECATED(10_0, 10_11, 2_0, 9_0, "Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.");
從 iOS9以後就廢棄這個方法使用下面新的方法
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters NS_AVAILABLE(10 _9, 7_0);
裡面的引數 allowedCharacters 有這麼幾種
- URLUserAllowedCharacterSet
user 使用者 - URLPasswordAllowedCharacterSet
password 密碼 - URLHostAllowedCharacterSet
host 主機 - URLPathAllowedCharacterSet
path 路徑 - URLQueryAllowedCharacterSet
query 問號 - URLFragmentAllowedCharacterSet
fragment 片段,碎片
我所用的 url 裡面包含片段,因此我用了URLFragmentAllowedCharacterSet,下面是扣德
NSURL *loadUrl = [NSURL URLWithString:[<#encoding url#> stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
———- 繼續更新