UIWebView與javascript互動二通過頁面的響應事件擷取頁面輸入框內的值

來源:互聯網
上載者:User

標籤:javascript   ios   html5   js   

接上篇文章,上篇文章實現了UIwebview對html檔案的載入和對js事件的響應,但是對html頁面上輸入的值沒有擷取。

今天就來說說這個首先要用到工具類WebViewJavascriptBridge 然後將工具類匯入到項目中。
其中test.js我做了如下的調整:

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)            var data = { ‘Javascript Responds‘:‘Wee!‘ }            log(‘JS responding with‘, data)            responseCallback(data)        })        bridge.registerHandler(‘testJavascriptHandler‘, function(data, responseCallback) {            log(‘ObjC called testJavascriptHandler with‘, data)            var responseData = { ‘Javascript Says‘:‘Right back atcha!‘ }            log(‘JS responding with‘, responseData)            responseCallback(responseData)        })        var str = document.getElementById(‘input1‘).value;//擷取輸入框的內容        bridge.callHandler(‘submit‘, str, function(response) {            log(‘JS got response‘, response)        })//將輸入框的內容通過回調 傳給OC頁面。實現互動    })}

另外對ViewController.m的內容作了如下修改:

////  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 ()@property WebViewJavascriptBridge* bridge;@end@implementation ViewController@synthesize myWebView;- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height-100)];    myWebView.delegate =self;    [self.view addSubview:myWebView];}- (void)viewWillAppear:(BOOL)animated {    if (_bridge) { return; }    [WebViewJavascriptBridge enableLogging];    _bridge = [WebViewJavascriptBridge bridgeForWebView:myWebView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {        NSLog(@"ObjC received message from JS: %@", data);    }];    [_bridge registerHandler:@"submit" handler:^(id data, WVJBResponseCallback responseCallback) {        NSLog(@"submit called: %@", data);//data就是submit被觸發後傳遞過來的資料    }];//    [_bridge send:@"A string sent from ObjC before Webview has 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 has loaded."];    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];    [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];}#pragma mark UIWebViewDelegate- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {    return true;}- (void)webViewDidStartLoad:(UIWebView *)webView{    NSLog(@"開始網頁");}- (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 {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

別的頁面都沒有改變。下面就開始運行吧運行結果如下:

終於可以互動了,哈哈,在接下來的文章中將會和大家一塊學習通過OC頁面傳值到html頁面中。
作為一名菜鳥在這一塊希望大家多多給予指導哦。^_^
差點忘了,這裡可以下載代碼:demo

UIWebView與javascript互動二通過頁面的響應事件擷取頁面輸入框內的值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.