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使用的更多相关文章
随机推荐
- Structs+Spring+Hibernate快速入门
Structs+Spring+Hibernate,这是很经典的一个搭配,它的开发流程也比较程式化,下面以用户注册为例. 注册页面代码: <%@page contentType=" ...
- mysql查找一个字段属于哪个表
查询mid字段存在于哪个表中 SELECT TABLE_SCHEMA,TABLE_NAME FROM information_schema.`COLUMNS` WHERE COLUMN_NAME='m ...
- 深度学习论文翻译解析(四):Faster R-CNN: Down the rabbit hole of modern object detection
论文标题:Faster R-CNN: Down the rabbit hole of modern object detection 论文作者:Zhi Tian , Weilin Huang, Ton ...
- shiro教程2(自定义Realm)
通过shiro教程1我们发现仅仅将数据源信息定义在ini文件中与我们实际开发环境有很大不兼容,所以我们希望能够自定义Realm. 自定义Realm的实现 创建自定义Realmjava类 创建一个jav ...
- windows下mongodb基础玩法系列一介绍与安装
windows下mongodb基础玩法系列 windows下mongodb基础玩法系列一介绍与安装 windows下mongodb基础玩法系列二CURD操作(创建.更新.读取和删除) windows下 ...
- [SCOI2012] 喵星球上的点名
Description 给定 \(N\) 个姓名串和 \(M\) 个点名串.询问每个点名串点到了多少姓名和每个姓名串被点到了几次.\(N\leq 5\cdot 10^4,M\leq 10^5\). S ...
- ASP .NET MVC HtmlHelper扩展——简化“列表控件”的绑定
在众多表单元素中,有一类<select>元素用于绑定一组预定义列表.传统的ASP.NET Web Form中,它对应着一组重要的控件类型,即ListControl,我们经常用到DropDo ...
- Asp.Net MVC3 简单入门详解过滤器Filter(转载)
前言 在开发大项目的时候总会有相关的AOP面向切面编程的组件,而MVC(特指:Asp.Net MVC,以下皆同)项目中不想让MVC开发人员去关心和写类似身份验证,日志,异常,行为截取等这部分重复的代码 ...
- [PHP] 算法-构建排除当前元素的乘积数组的PHP实现
构建乘积数组给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]. ...
- [PHP] 算法-两个n位的二进制整数相加问题PHP实现
两个n位二进制数分别存储在两个n元数组A和B中,这两个整数的和存在一个n+1元的数组C中答:此问题主要是考察相加进位的问题,元素1+1 =0 并且往前进一位ADD-BINARY(A,B) C=new ...