首页 > 编程 > JavaScript > 正文

iOS-JS交互(WebViewJavascriptBridge)

2019-11-14 18:36:25
字体:
来源:转载
供稿:网友
//
//  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(1041410035);
    messageButton.titleLabel.font = font;
    messageButton.backgroundColor = [UIColor colorWithWhite:1 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(11041410035);
    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(21041410035);
    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.vevb.com.cn/

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

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

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表