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

Source: Internet
Author: User

Origin

At the time of development, it is sometimes necessary to load a lot of webview, of course webview content can be local HTML can also be the content of the remote server. Choose which one you want with your needs.

Two days ago due to the need for development, it is necessary to convert an application A to a new application b,a and b Most of the functionality is the same, only B added some new features. So the code for B is based on a.

Some of the features in a need to call WebView, and clicking on some of the hyperlinks in WebView jumps to the corresponding page in a. For example, 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 to assume that the phone is loaded with a application. Will jump to the a application. If not installed, skip to the page specified by B. This is not the result we want. What about that? The most straightforward approach is to change the Web content of the server. Change all the hyperlink address a://** to b://**, but assume that the webview hypothesis is changed directly (that is, the content of a or B is returned after a or B is inferred directly from the server). But this interface has more than 10, assuming that change, will add server side very large workload, the eldest brother after thinking, decide or change bclient code, b webview in the click of the hyperlink address from a://** to b://**, This allows for the desired effect to be achieved with minimal effort.

Implement

When you click a hyperlink in UIWebView, you want to change the address of the hyperlink (which is, of course, a more unconventional operation.) But some tips can sometimes save a lot of time. The event for which you want to listen for hyperlinks in UIWebView. First you have to 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 redirected to this app page. Like http://www.baidu.com or something, so we need to find the hyperlink address to a://**.

if ([[URL scheme] isequaltostring:@ "Aichang"]) {}
Gets the hyperlink address. The next step is to change the URL scheme so that it becomes B. Code such as the following

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 collation such as the following:

-(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 bclient in the info in the URL schemes to B for example to


End

To the positive, to the odd win.

Change the server side of the Web page content is the most direct method, but the price is also some big, directly to the client click on the link address. Fast and easy. Why not? If there is any other opinion, please discuss it.

iphone Development-Change the link address when you click on 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.