我正在开发一个iOS应用,其中包含2个VC,mapVC(使用Google Map SDK)和webVC。在mapVC上选择一个注释,然后在打开相应网页的情况下过渡到webVC。
但是webView的viewDidLoad中的loadRequest方法太慢,无法等待。

因此,我想在mapView上选择图钉时预加载Web数据,然后使用loadData方法将预加载的数据移交给webVC。

但是webVC仅显示文本,不显示图像,不显示CSS。
我应该怎么做才能预加载完整的HTML / CSS /图像?
任何建议表示赞赏。提前致谢。

我当前的源代码如下。

// --- mapViewController ---
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
    NSURL   *url = [NSURL URLWithString:@"(target URL)"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                        timeoutInterval:60.0];
    receivedData = [NSMutableData dataWithCapacity:0];
    NSURLConnection *urlConnection;
    urlConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
    if (!urlConnection) {
        receivedData = nil;
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    receivedData = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showWeb"]) {
        WebViewController   *webViewController = [segue destinationViewController];
        webViewController.preloadedData = receivedData;
    }
}

// --- webViewController ---
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webView.delegate = self;
    [self.webView loadData:self.preloadedData
                  MIMEType:@"text/html"
          textEncodingName:@"utf-8"
                   baseURL:nil];
}

最佳答案

看看ASIWebPageRequest。它允许下载整个网页,包括所有资源。

10-08 20:22