IOS UIWebView get 404, 504 Error Solutions _ios

Source: Internet
Author: User
Tags deprecated error code

UIWebView get 404, 504 Error codes

Problem description

When using WebView, there is a problem:
If the access server returns an exception, such as 404, 504 error, you need to display a specific picture and copy on the native (404, 504 Purple error code is somewhat unsightly). So, the question comes, how can you know WebView access error, what is wrong???

Problem analysis

After the request was initiated from WebView, it was the agent who was able to view the WebView loading state, so we proceeded to analyze it from various proxy methods.

 WebView is called when the content is loaded, and return Yes is loaded
  -(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype;
  WebView has already started loading a request to invoke
  -(void) Webviewdidstartload: (UIWebView *) WebView;
  WebView
  -(void) Webviewdidfinishload: (UIWebView *) WebView after the end of the load request;
  Call
  -(void) WebView: (UIWebView *) WebView Didfailloadwitherror: (Nullable Nserror *) error in Request loading;

First of all, it must be from the Didfailloadwitherror proxy method, found that the request to 404 pages, and did not call the method, this is why? Originally, this method when the load process problems call, we successfully got 404 pages, it is not the loading process problem.

Then, from the Internet search the problem, found that the method provided by the user is to use the Sendsynchronousrequest method to return the Nshttpurlresponse status code to judge. However, there was a warning, sendsynchronousrequest after iOS9 was discarded, the new method of Datataskwithrequest agent.

' SendSynchronousRequest:returningResponse:error: ' is Deprecated:first deprecated in IOS 9.0-use [nsurlsession datatask Withrequest:completionhandler:]

The above is the way to get the status code, specific in which agent function to deal with, but also to look at:

Through the specific code analysis found that in Shouldstartloadwithrequest and Webviewdid finishload can get the corresponding status code, The status code that is placed on the webviewdidstartload is 0. After analysis found that the call to the Webviewdidstartload method, request requests have been initiated waiting for the server to process the results.

Problem solving

Finally, there are two ways to deal with the problem, namely Sendsynchronousrequest and Datataskwithrequest. The specific code is as follows:

 Methods one
  nshttpurlresponse *response = nil;
  [Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil];
  NSLog (@ "Statuscode:%ld", Response.statuscode);
   Method Two
  Nsurlsessiondatatask * datatask = [[Nsurlsession sharedsession] DataTaskWithRequest:webView.request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {
    Nshttpur Lresponse *tmpresponse = (nshttpurlresponse*) response;
    NSLog (@ "Statuscode:%ld", Tmpresponse.statuscode);
  }];
  [Datatask resume];

The status code can be obtained in both Shouldstartloadwithrequest and Webviewdidfinishload methods, which can be determined according to business requirements.

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.