Loading NSData into a UIWebView

后端 未结 1 1272
一整个雨季
一整个雨季 2020-12-19 02:54

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 U

相关标签:
1条回答
  • 2020-12-19 03:16

    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];
    }
    
    0 讨论(0)
提交回复
热议问题