将NSData加载到UIWebView中 [英] Loading NSData into a UIWebView

查看:202
本文介绍了将NSData加载到UIWebView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络浏览器中,我试图从<$ c $获得 NSData 加载 UIWebView C> NSURLConnection的。当我尝试将它加载到 UIWebView 中,而不是该网站时,它会显示HTML纯文本。



这里是我的代码: $ b $ viewDidLoad:

  NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:@http://www.msn.com]]; 
[NSURLConnection connectionWithRequest:request delegate:self];

后面的代码:


$ (NSURLConnection *)connection didReceiveData:(NSData *)data
{
webdata = [NSMutableData dataWithData:data] b $ b

  ; 

$ b - (void)connectionDidFinishLoading:(NSURLConnection *)连接
{
[webview loadData:webdata MIMEType:@text / htmltextEncodingName:@ UTF-8baseURL:nil];
}

解决方案

您不追加数据你正在接收。使用这段代码

   - (void)连接:(NSURLConnection *)连接didReceiveData:(NSData *)data {
if(webdata == nil){
webdata = [[NSMutableData alloc] init];
}
[webdata appendData:data];
}

根据您的数据长度,此方法可能会被调用一次或多次。因此,不要将新数据分配给您的ivar,而是将您的数据附加到您的数据中,以便您拥有完整的响应而不是收到的最后一个数据包。

------------------------------------------- -------------------------------------------------- ---------------------------------------

已更新

   - (void)connection:(NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)response {
webdata = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[webdata appendData:data];

$ b - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[mWebView loadData:webdata MIMEType:@text / htmltextEncodingName:@UTF-8基本URL:无];
}


In my web browser, I am trying to load a UIWebView with NSData obtained from a NSURLConnection. When I try to load it into the UIWebView, instead of the site, it comes up with the HTML plain text.

Here is my code:

in viewDidLoad:

NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.msn.com"]];
[NSURLConnection connectionWithRequest: request delegate:self];

later in the code:

 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
     webdata = [NSMutableData dataWithData: data];
  }

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
    [webview loadData:webdata MIMEType: @"text/html" textEncodingName: @"UTF-8" baseURL:nil];
  }

解决方案

You are not appending data that you are receiving. Use this piece of code

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    if (webdata == nil) {
        webdata = [[NSMutableData alloc] init];
    }
    [webdata appendData:data];
}

This method might be called once or more times depending upon your data length. So instead of assigning new data to your ivar, append your data to it so that you have the full response not the last packet of data received.
------------------------------------------------------------------------------------------------------------------------------------
Updated
Or use like this.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
        webdata = [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [webdata appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    [mWebView loadData:webdata MIMEType: @"text/html" textEncodingName: @"UTF-8" baseURL:nil];
}

这篇关于将NSData加载到UIWebView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆