关于 UIWebView 加载后为空白页

来源:互联网 发布:小小食杂铺 知乎 编辑:程序博客网 时间:2024/05/17 01:24

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]]];

———- 继续更新

1 0
原创粉丝点击