1. Objective-C calls the js method. You only need to call the method that comes with uiwebview.
[Webview stringbyevaluatingjavascriptfromstring: [nsstring stringwithformat: @ "gethtmlstring ('% @')", htmlstr];
2. When JS calls the objective-C method, a parameter or method needs to be agreed with Js.
-(Bool) webview :( uiwebview *) webview shouldstartloadwithrequest :( nsurlrequest *) Request navigationtype :( uiwebviewnavigationtype) navigationtype
In this callback method, we will get the data request sent from the server. We can determine the passed parameters and then determine the method for calling oC.
If ([request. URL. scheme isequaltostring: @ "gethtmlstring"]) {nsstring * htmlstr = [self gethtmlstring: description]; htmlstr = [htmlstr encoding: nsutf8stringencoding]; [webview encoding: [nsstring stringwithformat: @ "gethtmlstring (\" % @ \ ")", htmlstr];}
In this way, the OC is called.
Here is a particularly noteworthy question: I have uploaded parameters in HTML format. I have encountered it here. The parameters passed in are always incorrect and cannot call JS methods.
Stringwithcontentsoffile, which is used to read files. If no encoding method is specified, data may be lost. Therefore, we have specified the UTF-8 encoding method. There is another possible problem when passing parameters. If this parameter contains (\ r \ n') and so on, the JS side cannot receive this value, and these with \ needs to be escaped. The following code writes JavaScript to receive the complete message:
Message = [Message stringbyreplacingoccurrencesofstring: @ "\ n" withstring: @ "\\\\ N"]; nsstring * jsmethod = [nsstring stringwithformat: @ "jsmethod (\" % @ \ ")", message]
ExampleCode: Http://download.csdn.net/detail/pearlhuzhu/5550141