IPhone development-when you click the link address of UIWebView, you can change the link address.

Source: Internet
Author: User

IPhone development-when you click the link address of UIWebView, you can change the link address.
Origin

During development, you sometimes need to load a lot of webviews. Of course, the content of webview can be local html or remote server content, and you can choose which one to use as needed.

Two days ago, due to development needs, an application A needs to be converted into A new application B. Most of the functions of A and B are the same, but B adds some new functions, therefore, B's code is added based on. Some functions in A need to call webview. Clicking some hyperlinks in webview will jump to the corresponding page in A. For example, the hyperlink address is a: // app? Gocontrol1. In this case, the problem arises (of course, not the best excavator ?), What happens when a user clicks the hyperlink in the webview of application B? The answer is that if app A is installed on the mobile phone, the app will jump to app A. If app A is not installed, the app will jump to the page specified by app B. This is not the result we want. What should we do? The most direct method is to change the webpage content on the server and change all hyperlink addresses a: // ** to B ://**, however, if such A webview is few, it will be changed directly (that is, the content of A or B will be returned after the server determines A or B), but there are more than 10 such interfaces, if it is changed, it will increase the workload on the server side. After thinking about it, the boss decided to change the code of client B and set the URL of the hyperlink clicked in webview of client B to: // ** convert to B: // **. In this way, you can achieve the desired effect with minimal manpower.

Implementation

When clicking a hyperlink in UIWebView, you want to change the URL of the hyperlink (of course, this is an unconventional operation, but some tips sometimes save a lot of time ). To listen to events with hyperlinks in UIWebView, you must first set the delegate of UIWebView to the current Controller, that is

myBWebView.delegate = self;

Then find the callback of the click hyperlink of UIWebView.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {}
Then retrieve the URL of the hyperlink in the callback.

NSURL *url = request.URL;

Because the hyperlink can have other links not jump to the application page, such as http://www.baidu.com or something, so you need to find the hyperlink address with: // **

if ([[url scheme] isEqualToString:@"aichang"]) {}
After obtaining the hyperlink address, the next step is to change the url's scheme to 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];

Now, the new url is the url we need. The Code is 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;}


Change a in url schemes of info in client B to B, as shown in figure


Conclusion

Just win. Changing the webpage content on the server side is the most direct method, but the cost is also a little high. You can directly change the link address after the client clicks, which is fast, convenient, and simple. Why not? If you have other comments, you are welcome to discuss them.


Tutorial: IPhone development UIWebView loading local html has not been displayed

The pathForResource command is used to obtain the path from the system to the sandbox. Therefore, this path cannot be Chinese.
In addition, webView is not loaded to a certain view. How do you display it?
 
How to Control and change UIScrollView in UIWebView

The only subview of UIWebView is UIScrollView. However, Apple does not provide direct access to scrollview. Therefore, when you use scrollview directly, uiwebview. scrollview, no compilation error, but a prompt will be prompted during running
[UIWebView scrollView]: unrecognized selector sent to instance because webview's scrollview is an apple private api, we need to use some small trick to obtain its scrollview Code as follows: first we will pull the UIScrollView out from the UIWebView: NSArray * sv = [NSArray arrayWithArray: [myWebivew subviews];
UIScrollView * webScroller = (UIScrollView *) [sv objectAtIndex: 0]; this gives us full control over the built-in scrollview of webview. NSArray * webViewSubViews = [NSArray arrayWithArray: [infoWebView subviews) animated: YES]; when you set animated: YES, you can first move the animation back to the top, and then you can interrupt the original animation effect (if any) the result is the same as that of moving the statusbar to the top of the system.

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.