"IOS" page called JS and JS injection

Source: Internet
Author: User

Very many applications to save costs, to make the same time on Android and iOS can be used on the interface, then you need to use WebView to do. WebView is available on Android and iOS, making it easy to do. Of course, you should consider how you can interact with your Web pages in Android or iOS. For iOS, it contains how to invoke OC in a Web page and how to manipulate the Web page in OC.

Get your Web page into your iOS project first:

Web content such as the following, for testing only:


First, call JS in OC

The most important method:stringbyevaluatingjavascriptfromstring

Directly on the code, description see gaze:

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 nslog in the browser console input (@ "%@", [Self.webview stringbyevaluatingjavascriptfromstring:@ "Document.title"] ); 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 Web pages)*Method:is in the web linkWrite your own definition protocol..and then inOCmethod incheck the clicked linkwhether there is a protocol in the,there isdo the related operation.The code is as follows:

/**=========================== the proxy method for calling Oc===============================*//**webview in the WebView Link: loading the page. When the direct return is no, the JS method is called with the request parameter related to the sending of the requests */-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: ( Nsurlrequest *) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {NSLog (@ "%@", request. url.absolutestring); The URL that can be sent directly to send the request nsstring *URLSTR = Request. url.absolutestring;//format Neng://loadurl/blog.csdn.net Protocol/method/URL//Infer the protocol header in the link, assuming that 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]]; The method with the number of parameters, plus the colon sel callfun = nsselectorfromstring (funname);//Cancellation 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 reference # pragma clang diagnostic Popnslog (@ "method named%@, passed in as%@", Funname, Urls[1]); return NO; return YES;} -(void) Loadurl: (NSString *) urlstr {NSLog (@ "Received:%@", 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 talent 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 the head tag [Self.webview stringbyevaluatingjavascriptfromstring:@ "myFunction ();"];}




Attached sample source code: 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.