In the previous article, the previous article implemented the UIWebView loading of HTML files and the response to the JS event, but the values entered on the HTML page were not obtained.
Let's talk about this today. First Use the tool class Webviewjavascriptbridge and then import the tool class into the project.
Where test.js I made the following adjustments:
Window.onerror = function(err) {Log' Window.onerror: '+ Err)} function connectwebviewjavascriptbridge(callback) { if(Window. Webviewjavascriptbridge) {callback (Webviewjavascriptbridge)}Else{Document.addeventlistener (' Webviewjavascriptbridgeready ', function() {Callback (Webviewjavascriptbridge)},false) }} function check(){Connectwebviewjavascriptbridge ( function(bridge) {Bridge.init ( function(message, Responsecallback) {Log' JS got a message ', message)vardata = {' Javascript responds ':' wee! '} log (' JS responding with ', data) Responsecallback (data)}) Bridge.registerhandler (' Testjavascripthandler ', function(data, Responsecallback) {Log' OBJC called Testjavascripthandler with ', data)varResponseData = {' Javascript Says ':' right back atcha! '} log (' JS responding with ', ResponseData) Responsecallback (ResponseData)})varstr = document.getElementById (' INPUT1 '). Value;//Get the contents of the input boxBridge.callhandler (' Submit 'Str function(response) {Log' JS got response ', response)})//Pass the contents of the input box through the callback to the OC page. Implementing Interactions})}
In addition, the contents of VIEWCONTROLLER.M were modified as follows:
////VIEWCONTROLLER.M//Html5demo////Created by Jack on 15/3/27.//Copyright (c) 2015 Jack. All rights reserved.//#import "ViewController.h" #import "WebViewJavascriptBridge.h" @interface viewcontroller ()@propertyWebviewjavascriptbridge* Bridge;@end @implementation viewcontroller @synthesizemywebview;-(void) Viewdidload {[SuperViewdidload];additional setup after loading the view, typically from a nib.Mywebview = [[UIWebViewAlloc] Initwithframe:cgrectmake (0, -, Self. View. Frame. Size. Width, Self. View. Frame. Size. Height- -)]; Mywebview. Delegate= Self; [ Self. ViewAddsubview:mywebview];} - (void) Viewwillappear: (BOOL) Animated {if(_bridge) {return; } [Webviewjavascriptbridge enableLogging]; _bridge = [Webviewjavascriptbridge bridgeforwebview:mywebview webviewdelegate: Selfhandler:^ (IDData, Wvjbresponsecallback Responsecallback) {NSLog(@"OBJC received message from JS:%@", data); }]; [_bridge registerhandler:@"Submit"handler:^ (IDData, Wvjbresponsecallback Responsecallback) {NSLog(@"Submit called:%@", data);//data is the data that is passed when the submit is triggered.}];//[_bridge send:@ "A string sent from OBJC before Webview have loaded." responsecallback:^ (id responsedata) {//NSLog (@ "OBJC got response!%@", responsedata);// }];// //[_bridge callhandler:@ "Testjavascripthandler" data:@{@ "foo": @ "before ready"}];// // // //[_bridge send:@ "A string sent from OBJC after Webview have loaded."]; NSString*path = [[NSBundleMainbundle] pathforresource:@"Test"oftype:@"HTML"]; [Mywebview loadrequest:[nsurlrequestrequestwithurl:[NsurlFileurlwithpath:path]];}#pragma mark Uiwebviewdelegate- (BOOL) WebView: (UIWebView*) WebView Shouldstartloadwithrequest: (nsurlrequest*) Request Navigationtype: (Uiwebviewnavigationtype) Navigationtype {return true;} - (void) Webviewdidstartload: (UIWebView*) webview{NSLog(@"Start Page");} - (void) Webviewdidfinishload: (UIWebView*) webview{NSString*title = [WebView stringbyevaluatingjavascriptfromstring:@"Document.title"];NSLog(@"title=%@", title);//nsstring *st = [WebView stringbyevaluatingjavascriptfromstring:@ ' document.getElementById (' field_1 '). Value "]; NSString*st = [WebView stringbyevaluatingjavascriptfromstring:@"document.forms[0][' input1 '].value"];NSLog(@"Input1 =%@", ST);} - (void) WebView: (UIWebView*) WebView Didfailloadwitherror: (Nserror*) error{NSLog(@"Error%@", error);} - (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}@end
No other pages have changed. Let's run the following operation results:
Finally can interact, haha, in the next article will be with you to learn through the OC page value to the HTML page.
As a rookie in this piece hope that you give a lot of guidance oh. ^_^
Almost forgot, here can download code: Demo
UIWebView interacting with JavaScript two to get the value in the page input box via the response event of the page