使用UIWebView,网址加载完成之后,返回的数据显示乱码

- (void)viewDidLoad {

[super viewDidLoad];

self.webView = [[UIWebView alloc ] initWithFrame:CGRectMake(0, 0, kScreenWidth+80, kScreenHeight-64)];

self.webView.backgroundColor = [UIColor whiteColor];

self.webView.scrollView.bounces = YES;

self.webView.scrollView.delegate = self;

self.webView.delegate = self;

[self loadingDataForWebView];

[self.view addSubview:self.webView];

}

#pragma mark - webView delegate

- (void)webViewDidStartLoad:(UIWebView *)webView

{

[MBProgressHUD showHUDAddedTo:self.view animated:YES];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

[MBProgressHUD hideHUDForView:self.view animated:NO];

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

[MBProgressHUD hideHUDForView:self.view animated:NO];

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadingDataForWebView)];

//在label上面添加手势

[self.label addGestureRecognizer:tap];

[self.view addSubview:self.label];

}

#pragma mark - tap

- (void)loadingDataForWebView {

[self.label removeFromSuperview];

NSString * urlString = [NSString stringWithFormat:@"%@index.php/home/Api/faultdetail.html?id=%@", XRZUrlString, self.aid];

NSLog(@"aid 是什么鬼啊 |%@",urlString);

NSURL * url = [NSURL URLWithString:urlString];

//需要将网址进行转码,否则返回的数据为乱码

NSString * strUrl = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

[self.webView loadHTMLString:strUrl baseURL:url];

//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]]];

}

#pragma mark - lazy loading

- (UILabel *)label

{

if (_label == nil)

{

_label = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, kScreenWidth, 200)];

_label.text = @"网络不给力,点击重新加载";

_label.textColor = XRZredColor;

_label.userInteractionEnabled = YES;

}

return _label;

}

@end

你可能感兴趣的:(使用UIWebView,网址加载完成之后,返回的数据显示乱码)