"IOS" page called JS and JS injection

Source: Internet
Author: User

Many applications use WebView to save costs and to make an interface that can be used both on Android and iOS. There are webview on Android and iOS, which is easy to do. Of course, you should consider how you can interact with your Web pages in Android or iOS. For iOS, including how to call OC in a Web page, and how to manipulate a webpage in OC.

Get your Web page into your iOS project first:

The contents of the webpage are as follows, for testing only:


First, call JS in OC

The most important method:stringbyevaluatingjavascriptfromstring

Directly on the code, see note:

js====================================*/-(void) webviewdidfinishload is called in/**===========================oc: ( UIWebView *) WebView {//1.OC) The Document object called JS. (The properties of the document object are first spelled out) can be tested at the browser console input (@ "%@", [Self.webview stringbyevaluatingjavascriptfromstring:@ "Document.title"] NSLog ); The method of invoking JS in//2.oc [Self.webview stringbyevaluatingjavascriptfromstring:@ "ClickMe ()"];}

second, using hyperlinks to call the OC method

Steps:

*1.SetWebViewthe agent<uiwebviewdelegate>*2.methods in the proxyshouldstartloadwithrequest:called in.(This method is related to the loading of the Web page)*Method:is in the web linkwrite a custom protocol..and then inOCmethod incheck the clicked linkwhether there is a protocol in the,there isdo the related operation.The code is as follows:

/**=========================== WebView The proxy method that calls Oc===============================*//**webview in the link: Load page. When the direct return is no, the JS method is invoked where the request parameter is related to sending requests for */-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: ( Nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {NSLog (@ "%@", request. url.absolutestring); You can get direct access to the URL 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 perform the relevant operation if ([Urlstr hasprefix:@ "neng://"]) {//get rid of the rear of the protocol header nsstring *urlcontent = [urlstr substringfromindex:[@ "neng://" length]]; NSLog (@ "%@", urlcontent);//To split the string Nsarray *urls = [urlcontent componentsseparatedbystring:@ "/"]; NSLog (@ "Split result:%@", URLs);//Remove Method name if (Urls.count! = 2) {return NO;} NSString *funname = [NSString stringwithformat:@ "%@:", Urls[0]]; Method with parameters, add colon sel callfun = nsselectorfromstring (funname);//Cancel warning # pragma clang diagnostic push# pragma clang diagnostic IGN ORed "-warc-performselector-leaks" [Self performselector:callfun WITHOBJECT:URLS[1]]; Pass Blog.csdn.net as a parameter to # pragma clang diagnostic Popnslog (@ "method named%@, incoming parameter%@", Funname, Urls[1]); return NO; return YES;} -(void) Loadurl: (NSString *) urlstr {NSLog (@ "received parameter:%@", urlstr);//jump to the specified URL--->urlstrnsurl *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:

/**===========================js  injected ====================================*/-(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 tag [Self.webview stringbyevaluatingjavascriptfromstring:@ "myFunction ();"];}




Attached example Source: http://download.csdn.net/detail/xn4545945/7584575


Reprint Please specify Source: http://blog.csdn.net/xn4545945



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.