IOS Get UIWebView above button click event

Source: Internet
Author: User

UIWebView Some of the usage summary URLs http://blog.csdn.net/zhaoweixing1989/article/details/13021513

On the internet to see a lot of articles are said that iOS UIWebView more memory, in my project, I started with UIWebView to load the page. The first load is the HTML of their own stitching, and then add a button in the HTML, click and then use the current UIWebView to load the Web page. The program ran to find that the memory is very expensive, loading several web pages after the burst memory warning, do not handle the words can easily be evaluated to reject the app. Later it was changed to use the System Safari browser to open the Web page, the memory of the work to Apple's own browser to handle, to avoid my app crash. But how to get this button click on the page of the event, the button itself is bound to a URL, how to let it click on the current UIWebView not to load this URL.

Later looked up the information, found that the UIWebView protocol there is such a method, you can get it loaded on the page above the event, such as Click on the picture, click the button and so on.

-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: ( Uiwebviewnavigationtype) Navigationtype

{

Determines whether the click

if (Navigationtype = = uiwebviewnavigationtypelinkclicked)

{

Nsurl *url = [request URL];

if ([[UIApplication Sharedapplication]canopenurl:url])

{

[[UIApplication Sharedapplication]openurl:url];

}

return NO;

}

return YES;

}

The method of stitching the last added HTML button is as follows:

Nsurl * Path = [Nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@ "Source_page_button" ofType:@ "PNG"];

[Strreturn appendstring:[nsstring stringwithformat:@ "<div style= ' display:block;text-align:center;margin:0 auto; ' ><a href= '%@ ' ></a></div> ", _url,path]];

After this, clicking the button does not use the current UIWebView to load the Web page, but instead calls the system safari to open it.

Note: The last point to return yes, otherwise uiwebview just start a blank, this is because the first time the load is also UIWebView request a link, if return no, will not be loaded. You can also judge the content of the URL to see what the request is.

interface, if you add a delegate object for WebView and implement the interface, the method of the object is delegate before the WebView loads any frame, and the return value of the method is used to control whether the content of the target linked page is allowed to be loaded. Return yes to load the content directly, no to the contrary. And Uiwebviewnavigationtype enumeration that defines the classification of user behavior in the page, including

    • uiwebviewnavigationtypelinkclicked, the user struck a link.
    • uiwebviewnavigationtypeformsubmitted, the user submits a form.
    • Uiwebviewnavigationtypebackforward, the user strikes the forward or back button.
    • Uiwebviewnavigationtypereload, the user strikes the Reload button.
    • uiwebviewnavigationtypeformresubmitted, the user submits the form repeatedly
    • Uiwebviewnavigationtypeother, other behavior occurs.

IOS Get UIWebView above button click event

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.