Get your Web page into your iOS project first:
The contents of the webpage are as follows, for testing only:
[HTML]View PlainCopy
- <html>
- <head>
- <meta xmlns= "http://www.w3.org/1999/xhtml" http-equiv="Content-type " content="text/html; Charset=utf-8 " />
- <title> This is a sample HTML file </title>
- <script type=' text/javascript '>
- function ClickMe () {
- Alert (' Click the button! ‘);
- }
- </Script>
- </head>
- <body>
- <H1>oc and JS Interactive </H1>
- <h2>blog.csdn.net/xn4545945</h2>
- < interact with OC!--Custom Protocol --
- <a href="neng://loadurl/blog.csdn.net"> click, Link calls OC function </a>
- <br/>
- <br/>
- <a href="http://m.baidu.com">js injection, to the Baidu page experiment </a>
- </body>
- </html>
First, call JS in OC
The most important method: Stringbyevaluatingjavascriptfromstring
Directly on the code, see note:
[OBJC]View PlainCopy
- Call js====================================*/in/**===========================oc
- -(void) Webviewdidfinishload: (UIWebView *) WebView {
- the Document object in the//1.oc called JS. (The properties of the document object are first spelled out) can be tested in the browser console input
- NSLog (@ "%@", [self. WebView stringbyevaluatingjavascriptfromstring:@ "Document.title"]);
- method of invoking JS in//2.oc
- [self. WebView stringbyevaluatingjavascriptfromstring:@ "ClickMe ()"];
- }
Second, using hyperlinks to call the OC method
Steps:
* * Set WebView proxy <uiwebviewdelegate>*2. Called in the agent's method Shouldstartloadwithrequest:. (This method is related to the loading of the Web page) * Method: is to write a custom protocol in the page link. Then in the OC method, check whether there is a link in the click, and then do the relevant action.
The code is as follows:
[OBJC]View PlainCopy
- /**=========================== WebView link in call oc===============================*/
- Agent method for/**webview: Loading page. When returned directly to No, the JS method is called
- Where the request parameter is related to sending requests */
- -(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (nsurlrequest *) Request Navigationtype: (uiwebviewnavigationtype) navigationtype {
- NSLog (@ "%@", request.) URL. absolutestring); //You can get the URL to send the request directly
- nsstring *urlstr = Request. URL. absolutestring;
- //Format Neng://loadurl/blog.csdn.net Protocol/method/URL
- //Determine the protocol header in the link, and if it is neng://, then do the relevant operation
- if ([Urlstr hasprefix:@ "neng://"]) {
- //Get rid of the back of the protocol header
- nsstring *urlcontent = [urlstr substringfromindex:[@ "neng://" length]];
- NSLog (@ "%@", urlcontent);
- //use/to split the string
- Nsarray *urls = [urlcontent componentsseparatedbystring:@ "/"];
- NSLog (@ "Split results are:%@", URLs);
- //Remove method name
- if (URLs. Count! = 2) {
- return NO;
- }
- nsstring *funname = [NSString stringwithformat:@ "%@:", urls[0]]; //method with parameters, plus a colon
- SEL callfun = nsselectorfromstring (funname);
- Cancel Warning
- # pragma clang diagnostic push
- # pragma clang diagnostic ignored "-warc-performselector-leaks"
- [self performselector:callfun withobject:urls[1]; //Pass the blog.csdn.net as a parameter
- # pragma clang diagnostic pop
- NSLog (@ "method named%@, incoming parameter is%@", Funname, urls[1]);
- return NO;
- }
- return YES;
- }
- -(void) Loadurl: (NSString *) urlstr {
- NSLog (@ "received parameter:%@", URLSTR);
- //Jump to the specified URL--->urlstr
- Nsurl *url = [Nsurl urlwithstring:[nsstring stringwithformat:@ "http://%@", Urlstr]];
- nsurlrequest *request = [nsurlrequest requestwithurl:url];
- [self. WebView Loadrequest:request];
- }
Third, JS implementation injection
JS is the operation of the Web page, the use of good JS to really in the application of the arbitrary operation of the Web page.
Core method: Stringbyevaluatingjavascriptfromstring (same as the first one, this is the most important way to operate JS in iOS)
The code is as follows:
[OBJC]View PlainCopy
- /**===========================JS Injection ====================================*/
- -(void) Jsclick {
- [self. WebView stringbyevaluatingjavascriptfromstring:@ "var script = document.createelement (' Script '); "
- "Script.type = ' text/javascript ';"
- "script.text = \" Function myFunction () {" //define MyFunction method
- "var field = document.getelementsbyname (' word ') [0];"
- "field.value= ' WWDC2014 ';"
- "Document.forms[0].submit ();"
- "}\";"
- "document.getElementsByTagName (' head ') [0].appendchild (script);"]; //Add to Head tab
- [self. WebView stringbyevaluatingjavascriptfromstring:@ "myFunction ();"];
- }
attached example Source: http://download.csdn.net/detail/xn4545945/7584575
iOS development--Invoke JS and JS injection in Web pages