TGJSBridge使用
、在ViewController.h中
#import <UIKit/UIKit.h>
#import "TGJSBridge.h"
@interface BaseViewController : UIViewController<TGJSBridgeDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate,UIWebViewDelegate>
@property(nonatomic,strong)TGJSBridge *jsBridge;
@property(nonatomic,strong)UILabel *btnLabel;
@end
、在ViewController.m中
#import "BaseViewController.h"
@interface BaseViewController ()
{
UIWebView *webView;
UIImagePickerController *picker;
UIPopoverController *popPicture;
}
@end
@implementation BaseViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//UIWebView初始化
webView = [[UIWebView alloc] initWithFrame:CGRectMake(, , , )];
webView.layer.borderWidth = ;
NSURL *url = [[NSBundle mainBundle] URLForResource:@"demo" withExtension:@"html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
//TGJSBridge配置
self.jsBridge = [TGJSBridge jsBridgeWithDelegate:self];
webView.delegate = self.jsBridge;
// [self.jsBridge postNotificationName:@"demo" userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"before load",@"message", nil] toWebView:webView];
//UILabel初始化
self.btnLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
self.btnLabel.backgroundColor = [UIColor redColor];
self.btnLabel.text = @"我要变身"
//UIImagePickerView初始化
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[webView reload];
[self.view addSubview:webView];
[self.view addSubview:self.btnLabel];
[self.view addSubview:webView];
}
#pragma mark - TGJSBridgeDelegate
-(void)jsBridge:(TGJSBridge *)bridge didReceivedNotificationName:(NSString *)name userInfo:(NSDictionary *)userInfo fromWebView:(UIWebView *)webview
{
NSLog(@"%@",name);
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
self.btnLabel.text = [userInfo objectForKey:@"message"];
}
#pragma mark - UIImagePickerViewControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *url =[info objectForKey:UIImagePickerControllerReferenceURL];
NSLog(@"%@",url);
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:];
[dic setValue:@"" forKey:@"message"];
[self.jsBridge postNotificationName:@"demo" userInfo:dic toWebView:webView];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
在demo.html中
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JSBridge Test</title>
<script src="./TGJSBridge.bundle/TGJSBridge.js"></script>
<script src="jquery.min.js"></script>
<body>
<div style="margin-top:50px;">
<input type="button" name="" value="点我" id="ss" onclick="process()" />
<img src="1.jpg" id = "image">
</div>
<script type="text/javascript">
function log(text){
alert(text);
}
var click_count = ;
function process()
{
alert();
jsBridge.postNotification('oc',{message:'hello oc:'+click_count++});
}
jsBridge.bind('demo', function(object){
log(object.message);
// alert(1);
});
</script>
</body>
</html>
附TGJSBridge git地址:https://github.com/ohsc/TGJSBridge
TGJSBridge使用的更多相关文章
随机推荐
- leetcode — divide-two-integers
/** * Source : https://oj.leetcode.com/problems/divide-two-integers/ * * Created by lverpeng on 2017 ...
- U3D GameObject 解读
GameObject本身没有功能,是Unity场景里所有组件的基类,但很多时候我们需要在脚本中操作GameObject.先讲一下GameObject类包含哪些内容,其中常用的用红色标出了 Variab ...
- SPI 驱动框架
SPI 驱动框架 1. 枚举过程 drivers/spi/spi.c: spi_register_board_info /* 对于每一个spi_master,调用spi_match_master_to ...
- Python 的几种推导式
推导式 comprehensions(又称解析式):是 Python 中很强大的.很受欢迎的特性,具有语言简洁,速度快等优点.推导式包括: 1. 列表推导式 2. 字典推导式 3. 集合推导式 对以上 ...
- Java基础——Oracle(八)
一.流程控制语句 1) 循环语句 == loop .. end loop 简单的循环,至少被执行一次 create table userinfo (id number, name varchar2( ...
- 【IDEA&&Eclipse】5、IntelliJ IDEA常见配置
[idea配置jdk] http://blog.csdn.net/tolcf/article/details/50803414 [idea intellij 如何配置tomcat]http://jin ...
- 【hibernate】1、Hibernate的一个注解 @Transient
@Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性.如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic ...
- 撩课-Java每天5道面试题第17天
116.说下Struts的设计模式 MVC模式: web应用程序启动时 就会加载并初始化ActionServler. 用户提交表单时, 一个配置好的ActionForm对象被创建, 并被填入表单相应的 ...
- 设计模式之命令模式(Command )
命令模式是我们能够实现发送者和接收者之间的完全解耦,发送者是调用操作的对象,而接收者是接收请求并执行特定操作的对象.通过解耦,发送者无需了解接收者的接口.在这里,请求的含义是需要被执行的命令. 作用 ...
- Python带你轻松进行网页爬虫
前不久DotNet开源大本营通过为.NET程序员演示如何在.NET下使用C#+HtmlAgilityPack+XPath进行网页数据的抓取,从而为我们展示了HtmlAgilitypack利器的优点和使 ...