iOS-JS交互 (WebViewJavascriptBridge)

//
//  ViewController.m
//  JSAndIOS交互
//
//  Created by apple on 15/7/31.
//  Copyright (c) 2015年 tqh. All rights reserved.
// #import "ViewController.h"
#import "WebViewJavascriptBridge.h"
@interface ViewController ()<UIWebViewDelegate>
@property WebViewJavascriptBridge* bridge;
@end @implementation ViewController - (void)viewDidLoad {
    [super viewDidLoad];
    if (_bridge) { return; }
    
    UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:webView];
    
    [WebViewJavascriptBridge enableLogging];
    
    //网页交互,网页发送消息给程序
    _bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"从JS收到消息: %@", data);
        responseCallback(@"back");
    }];
    //网页交互,网页发送消息给程序
    [_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"testObjcCallback called: %@", data);
        responseCallback(@"back");
    }];
    [self renderButtons:webView];
    [self loadExamplePage:webView];
} - (void)renderButtons:(UIWebView*)webView {
    UIFont* font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
    
    UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [messageButton setTitle:@"发送消息" forState:UIControlStateNormal];
    [messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:messageButton aboveSubview:webView];
    messageButton.frame = CGRectMake(, , , );
    messageButton.titleLabel.font = font;
    messageButton.backgroundColor = [UIColor colorWithWhite: alpha:0.75];
    
    UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [callbackButton setTitle:@"调用处理程序" forState:UIControlStateNormal];
    [callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:callbackButton aboveSubview:webView];
    callbackButton.frame = CGRectMake(, , , );
    callbackButton.titleLabel.font = font;
    
    UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [reloadButton setTitle:@"重新加载webview" forState:UIControlStateNormal];
    [reloadButton addTarget:webView action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:reloadButton aboveSubview:webView];
    reloadButton.frame = CGRectMake(, , , );
    reloadButton.titleLabel.font = font;
} //加载网页
- (void)loadExamplePage:(UIWebView*)webView {
    //ExampleApp加载本地网页
    NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
    NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
    [webView loadHTMLString:appHtml baseURL:baseURL];
} #pragma mark - 事件监听 - (void)sendMessage:(id)sender {
    //发送消息
    [_bridge send:@"oc发送消息" responseCallback:^(id response) {
        NSLog(@"sendMessage got response: %@", response);
    }];
} - (void)callHandler:(id)sender {
    //发送消息
    id data = @{ @"name": @"value" };
    [_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
        NSLog(@"testJavascriptHandler responded: %@", response);
    }];
} #pragma mark - UIWebViewDelegate - (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"webViewDidStartLoad");
} - (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"webViewDidFinishLoad");
}
@end

github下载:https://github.com/marcuswestin/WebViewJavascriptBridge

百度云下载:http://pan.baidu.com/s/1kTnbpTd

js学习:http://www.w3school.com.cn/

http://www.fenesky.com/blog/2014/04/24/how-javascriptcore-works.html

http://www.cnblogs.com/ider/p/introduction-to-ios7-javascriptcore-framework.html

webkit学习:http://blog.csdn.net/column/details/webkit-in-deep.html

上一篇:读和写,关于cache和buffer


下一篇:vijosP1159 岳麓山上打水