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使用的更多相关文章
随机推荐
- 分布式系统CAP理论以及注册中心选择
CAP定理:指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可同时获得. 一致性(C-数据 ...
- 第一个Quartz程序 (二)
1 我们使用maven项目 2 创建一个job类,在execute()方法里写上业务逻辑代码. 3 在另外一个类中创建触发器,调度器,并且绑定job. 首先在项目的pom.xml引入需要的jar包. ...
- WebService与RMI(远程调用方式实现系统间通信)
前言 本文是<分布式java应用基础与实践>读书笔记:另外参考了此博客,感觉讲的挺好的,尤其是其中如下内容: 另外,消息方式实现系统间通信本文不涉及.RMI则只采用spring RMI框架 ...
- mybatis教程6(逆向工程)
什么是逆向工程 简单点说,就是通过数据库中的单表,自动生成java代码. Mybatis官方提供了逆向工程,可以针对单表自动生成mybatis代码(mapper.java\mapper.xml\po类 ...
- WebFrom 小程序【条件查询】
实现按照各种条件对数据库进行综合查询 基本功能:可以根据用户需要灵活查询 重难点:各种条件的可能.限制. public List<users> selectA( string str,Ha ...
- Java框架之Struts2(一)
在学习Struts2之前,我们要知道Java为什么要有反射机制呢?反射机制可以说是填补Java不能动态访问某一个类的空白.利用反射机制,动态的创建一个对象.动态的访问类的某个属性,而且访问哪一个属性自 ...
- JAVA的高并发基础认知 一
一.多线程的基本知识 1.1进程与线程的介绍 程序运行时在内存中分配自己独立的运行空间,就是进程 线程:它是位于进程中,负责当前进程中的某个具备独立运行资格的空间. 进程是负责整个程序的运行,而线程是 ...
- Breeze 部署 Kubernetes 1.12.1高可用集群
今天看文章介绍了一个开源部署 K8S 的工具,有空研究下~ Github 地址: https://github.com/wise2c-devops/breeze
- JqGrid: Add,Edit,Del in asp.net
https://github.com/1rosehip/jplist https://github.com/free-jqgrid/jqGrid https://plugins.jquery.com/ ...
- 洛谷P3899 [湖南集训]谈笑风生(线段树合并)
题意 题目链接 Sol 线段树合并板子题,目前我看到两种写法,分别是这样的. 前一种每次需要新建一个节点,空间是\(O(4nlogn)\) 后者不需要新建,空间是\(O(nlogn)\)(面向数据算空 ...