iphone Development-Change the link address when you click on the UIWebView link address

Source: Internet
Author: User

Origin

In the development, sometimes need to load a lot of webview, of course webview content can be local HTML can also be the content of the remote server, with the need to choose which one to use.

The first two days because of the need for development, the need to convert an application A to a new application b,a and B Most of the features are the same, but B added some new features, so B's code is based on a to add. A some of the features need to call WebView, and clicking on some hyperlinks in WebView will jump to the corresponding page in a, such as the address of the hyperlink is a://app?gocontrol1. This is the problem (of course, not learning excavator which strong?) What happens when the user clicks the hyperlink in the WebView of the B app? The answer is that if the phone has a application, it will jump to the a application, if not installed jump to b specified page, the application is jumping, this is not the result we want. What about that? The most direct way is to change the service side of the Web page content, all the hyperlink address a://** to b://**, but if such a webview if less directly changed (that is, directly through the server to determine a or B after the content of A or b), but this interface has more than 10, if changed, will increase the server side a lot of work, the eldest brother after thinking, decided to change the B client's code, the B's WebView in the click of the hyperlink address from a://** to b://**, so that you can achieve the desired effect by the minimum manpower.

Implement

When you click on a hyperlink in UIWebView, you want to change the address of the hyperlink (which is, of course, quite a bit of a rule, but some tricks can sometimes save a lot of time). To listen for the UIWebView in a hyperlink, first set the UIWebView delegate as the current controller, i.e.

Mybwebview.delegate = self;

Then find the callback for UIWebView's click Hyperlink

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype {}
Then remove the address of the hyperlink in the callback

Nsurl *url = Request. URL;

Because hyperlinks can have other link addresses that are not jump to this app page, such as http://www.baidu.com, you need to find the hyperlink address to a://**

if ([[URL scheme] isequaltostring:@ "Aichang"]) {}
Get the hyperlink address, the next step is to change the URL scheme, so that it becomes B, the code is as follows

NSString *newurlstring =  [NSString stringwithformat:@ "b://%@%@",   url.host, Url.path];if (url.query) { newurlstring = [newurlstring stringbyappendingformat:@ "?%@", Url.query];} url = [Nsurl urlwithstring:newurlstring];

OK, the new URL is the URL we need, the whole code is organized as follows:

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype {//Determine if we want the system to handle it. Nsurl *url = Request. Url;if ([[[URL scheme] isequaltostring:@ "a"]) {nsstring *newurlstring =  [NSString stringwithformat:@ "b://%@%@",   Url.host, Url.path];if (url.query) {newurlstring = [newurlstring stringbyappendingformat:@ "?%@", Url.query];} url = [Nsurl urlwithstring:newurlstring];if ([[[UIApplication Sharedapplication]canopenurl:url]) {[[UIApplication Sharedapplication]openurl:url];return NO;}} return YES;}


Also change the URL schemes in the info in the B client to B as


End

To the positive, to the odd win. Change server-side page content is the most direct method, but the price is also some big, directly to the client after the click of the link address, quick and easy, why not? If there are other comments, please feel free to discuss them.

Iphone Development-Change the link address when you click the UIWebView link address

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.