WebView redirect New Open Interface problem-B

Source: Internet
Author: User
Tags response code

First introduce this problem, iOS WebView if want to be closer to native, will load the new URL when the new interface, but if the load of the link is redirected, it will open a blank interface in the middle, this is very annoying. Then it is to solve this problem and adopt a lot of methods. The redirected response code is 301, so work in this direction. First found a method on the Internet. Original connection: http://blog.csdn.net/sheldongreen/article/details/7977802#

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response 

This is a method under the Nsurlconnectiondatadelegate.
Usage is Jiangzi drop

- (void) Viewdidload {[Super Viewdidload];Nsurl *url = [ nsurl urlwithstring:@ "http://www.google.com"]; nsurlrequest *request = [nsurlrequest Requestwithurl:url]; nsurlconnection *connection = [[nsurlconnection alloc] InitWithRequest: Request Delegate:self]; }-(nsurlrequest *) connection: (nsurlconnection *) Connection willsendrequest: (nsurlrequest *) Request Redirectresponse: (Nsurlresponse *) Response {nslog (@ "would send request\n%@", [request URL]); Span class= "hljs-built_in" >nslog (@ "redirect response\n%@", [response URL]); return request; } 

This is not the end of the test, but it works.

====will Send request====http://www. google. com/====redirect response==== (null) ====will send request====http://www. google. com. hk/url?sa=p&hl=Zh-cn&pref=hkredirect&pval=yes&q=http://www. google. com. hk/&ust=1347589441099727&USG=AFQJCNESBYCRLH_JQPBWRWSC0NPBJ2_LFG ====redirect response==== Span class= "Hljs-label" >http://www.google.com/= ===will send request==== http://www.google.com.hk/====redirect response====  Http://www.google.com .hk/url?sa=p&hl=zh-cn&pref=hkredirect& Pval=yes&q=http://www.google.com.hk/&ust=1347589441099727&usg=afqjcnesbycrlh_ JQPBWRWSC0NPBJ2_LFG              

But this uncertainty is redirection or intra-interface jump, and the new interface is not very good to do (interested can try).
Pass it off.

Then the friend introduced the second method, found on the http://stackoverflow.com. Attach the original connection Http://stackoverflow.com/questions/13505889/how-to-get-redirected-url.

-(void)webViewDidStartLoad:(UIWebView *)webView{    NSURL *url = [webView.request mainDocumentURL];    NSLog(@"The Redirected URL is: %@", url);}

is still the same as the same question. Pass it off.

Finally, a third method was found. Http://stackoverflow.com/questions/1061364/how-do-i-get-the-last-http-status-code-from-a-uiwebview
The principle of this is that the principle is to request and then load with WebView.

- (BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) navigationtype{if (navigationtype! = uiwebviewnavigationtypeother) {Self. Loadedurl = Request. Url. absolutestring; }if (!isload && [Request. Url. Absolutestring Isequaltostring:loadedurl]) {[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data,Nserror *connectionerror) {if (Connectionerror | | ([Response Respondstoselector:@selector (StatusCode)] && [(nshttpurlresponse *) response) StatusCode]! =&& [((Nshttpurlresponse *) response) StatusCode]! =302)) {Show error message [Self showerrormessage]; }else {isload =YES; [Self showpage:absolutestring query:NIL]; } }];ReturnNO; } isload =NO;ReturnYES;}New Open Interface-(void) ShowPage: (nsstring *) URL query: (nsstring *) query{nsstring * newurl = [nsstring stringwithformat:@  "%@", url]; if (query) {newurl = [nsstring Stringwithformat:@ "%@?%@", url,query]; } Jfwebviewcontroller *webview = [[Jfwebviewcontroller alloc]init]; Webview.weburl = Newurl; [self.navigationcontroller Pushviewcontroller:webview Animated:yes];} -(void) Viewwillappear: (bool) animated{_isLoad = NO; [super viewwillappear:animated];}         

Perfect solution.

WebView redirect New Open Interface problem-B

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.