How to stop UIWebView loading immediately

后端 未结 2 1506
一生所求
一生所求 2021-01-03 06:23

As ios documentation says, [webView stopLoading] method should be used in order to stop webview load task.

As far as I see, this methods runs asynchron

相关标签:
2条回答
  • 2021-01-03 06:44

    Don't use the UIWebView to download your html document directly. Use async download mechanism like ASIHTTPRequest to get your html downloaded by a background thread. When you get the requestFinished with a content of your html then give it to the UIWebView.

    Example from the ASIHTTPRequest's page how to create an asynchronous request:

    - (IBAction)grabURLInBackground:(id)sender
    {
       NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
       ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
       [request setDelegate:self];
       [request startAsynchronous];
    }     
    
    - (void)requestFinished:(ASIHTTPRequest *)request
    {
       // Use when fetching text data
       NSString *responseString = [request responseString];
     
       // Use when fetching binary data
       NSData *responseData = [request responseData];
    }     
    
    - (void)requestFailed:(ASIHTTPRequest *)request
    {
       NSError *error = [request error];
    }
    

    Use the responseString to your UIWebView's loadHTMLString method's parameter:

    UIWebView *webView = [[UIWebView alloc] init];
    [webView loadHTMLString:responseString baseURL:[NSURL URLWithString:@"Your original URL string"]];
    
    0 讨论(0)
  • 2021-01-03 06:52

    This worked for me.

    if (webText && webText.loading){
        [webText stopLoading];
    }
    
    webText.delegate=nil;
    
    
    NSURL *url = [NSURL URLWithString:@""];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webText loadRequest:requestObj];
    
    0 讨论(0)
提交回复
热议问题