In objective-C interaction between iOS development and JavaScript, we can useStringbyevaluatingjavascriptfromstring to obtain relevant node attributes in obj-C and add JavascriptCode. But how can we monitorWhat about JavaScript response events. Effective API implementation in Mac OS, But iPhone does not, but we have a way to do this:
The general idea is to setDocument. LocationThis will lead to a delegate method of webview, so as to achieve the effect of sending notifications, that is, to achieve the purpose of listening.
1. Define a protocol agreement between JavaScript and webview:
MyApp: myfunction: myparam1: myparam2
2. Add code in javascript:
Document. Location = "MyApp:" + "myfunction:" + param1 + ":" + param2;
3. Delegate method in webviewWebview: shouldstartloadwithrequest: navigationtype:Add
-(Bool) webview :( uiwebview * ) Webview2 shouldstartloadwithrequest :( nsurlrequest * ) Request navigationtype :( uiwebviewnavigationtype) navigationtype {nsstring * Requeststring = [[Request URL] absolutestring]; nsarray * Components = [requeststring componentsseparatedbystring: @" : " ]; If ([Components count]>1 && [(Nsstring *) [Components objectatindex: 0 ] Isequaltostring: @" MyApp " ]) { If ([(Nsstring *) [components objectatindex: 1 ] Isequaltostring: @" Myfunction " ]) {Nslog ([components objectatindex: 2 ]); // Param1 Nslog ([components objectatindex: 3 ]); // Param2 // Call your method in objective-C method using the above... } Return No ;} Return Yes; // Return YES to make sure regular navigation works as expected. }
Check: http://stackoverflow.com/questions/5671742/send-a-notification-from-javascript-in-uiwebview-to-objectivec
Http://www.codingventures.com/2008/12/using-uiwebview-to-render-svg-files/