Uiwebview and objective-C

Source: Internet
Author: User

Using oC to call JS is simple. The system provides the stringbyevaluatingjavascriptfromstring method.

 
[Webview stringbyevaluatingjavascriptfromstring: @ "document. Location. href"]; [webview stringbyevaluatingjavascriptfromstring: @ "jsmethod ()"];

In turn, it seems impossible. OC does not provide a direct method, but it uses some tips of webview to achieve the goal!

Webview provides the following proxy methods:

-(Bool) webview :( uiwebview *) webview shouldstartloadwithrequest :( nsurlrequest *) Request navigationtype :( uiwebviewnavigationtype) navigationtype;
-(Void) webviewdidstartload :( uiwebview *) webview;
-(Void) webviewdidfinishload :( uiwebview *) webview;
-(Void) webview :( uiwebview *) webview didfailloadwitherror :( nserror *) error;

The method is simple, as the name suggests! Request to start/loading started/Loading completed/loading error

Shouldstartloadwithrequest can be used to capture all requests of uiwebview! If we send a specific request in JS that contains the OC Function and request parameters to be called, in this way, we can use this proxy method to capture and match it, And then manually call it to achieve the goal ??? Yes, it works.

For example, JavaScript can be written as follows:

 
Function sendcommand (CMD, Param) {var url = "Protocol: //" + cmd + ":" + Param; document. Location = URL ;}

Protocol is a self-defined protocol. As long as it does not conflict with HTTP, https, and other international protocol headers, the name can be obtained at will;

CMD is the OC method to be called;

Param is the called parameter.

Then use document. location to achieve the goal! The above"Protocol ://"+ Cmd +":"+ Param is a custom request format, which can also be changed, for example, as shown below

Document. Location = "mycustomprotocolname: functionname? Param1 = value1 & param2 = value2 ...".

How can I define them in JS? How can I help it! However, after this request is captured in the shouldstartloadwithrequest method, you need to parse the request to determine whether it is your own specific request!

-(Bool) webview :( uiwebview *) webview preview :( nsurlrequest *) Request navigationtype :( uiwebviewnavigationtype) navigationtype {nsstring * rurl = [[request URL] absolutestring]; If ([rurl hasprefix: @ "Protocol: //"]) {// if it is a custom protocol, intercept the methods and parameters in the protocol, call the OC method manually here after the judgment is correct} return true ;}

Note: The stringbyevaluatingjavascriptfromstring method has a place to pay attention to. It is not a bug, but it does have a problem. Pay attention to it!

If stringbyevaluatingjavascriptfromstring executes a JS function with parameters, if this parameter contains (\ r \ n') and so on, the JS side will not receive this value, and the escape characters with \ are required, the following code writes JavaScript to receive the complete message:

Message = [Message stringbyreplacingoccurrencesofstring: @ "\ n" withstring: @ "\\\\ N"];

Nsstring * jsmethod = [nsstring stringwithformat: @ "jsmethod (\" % @ \ ")", message];

There is another place where the two statements seem the same and actually different. If you use 'quotation marks, if the message carries ', the message cannot be passed! Therefore, it is best to use"

Nsstring * jsmethod = [nsstring stringwithformat: @ "jsmethod (\" % @ \ ")", message];

Nsstring * jsmethod = [nsstring stringwithformat: @ "jsmethod ('% @')", message];

In addition, there are examples of open-source JS communication with webview for use. There are also the problems mentioned above, which need to be modified by yourself!

Https://github.com/marcuswestin/WebViewJavascriptBridge

Related Article

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.