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使用的更多相关文章
随机推荐
- Centos7安装Mysql8(官方整合包)
1. 下载整合包 [root@master ~]# wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.12-1.el7.x86_ ...
- leetcode — longest-substring-without-repeating-characters
import java.util.HashSet; import java.util.Set; /** * Source : https://oj.leetcode.com/problems/long ...
- C# 元数据描述
元数据概述:元数据是一种二进制信息,用以对存储在公共语言运行库可移植可执行文件 (PE) 文件或存储在内存中的程序进行描述.将您的代码编译为 PE 文件时,便会将元数据插入到该文件的一部分中,而将代码 ...
- EF SaveChanges() 报错(转载)
最佳答案 报这个错是因为,提交了主键重复的数据,虽然未提交到数据库中 但是现在的EF上下文中已经包含了我提交的数据,下次在提交正确数据时, 原来添加到上下文中的数据依然还在..如何处理这个问题呢? ...
- Navicat安装及简单使用
一.安装 下载完之后,直接解压出来就能用,看一下解压之后的目录: 双击打开下面这个文件(可以把它添加一个桌面快捷方式,或者添加到任务栏): 然后会提示你输入注册码: 回到navicat的解压出来的文件 ...
- node.js入门学习笔记整理
(1)node Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node与javaScript的区别在于,javaScript的顶层对象是window,而no ...
- git命令详解(一)
今天我们来详解一下git的各种命令,此为git的第一篇,后续还会有好几篇,希望大家看了能有所进步 第一篇的命令 1.git commit 2.git branch 3.git merge 4.git ...
- maven 技巧
M2Eclipse Releases maven eclipse插件最新安装地址 Full Version Tag 1.0 2011-06-22 http://download.eclipse.org ...
- IDEA项目搭建六——使用Eureka和Ribbon进行项目服务化
一.Eureka的作用 这里先简单说明使用eureka进行业务层隔离,实现项目服务化也可以理解为微服务,我一直崇尚先实现代码再学习理论,先简单上手进行操作,eureka使用分为三块,1是服务注册中心, ...
- Android Studio 使用ViewPager + Fragment实现滑动菜单Tab效果 --简易版
描述: 之前有做过一个记账本APP,拿来练手的,做的很简单,是用Eclipse开发的: 最近想把这个APP重新完善一下,添加了一些新的功能,并选用Android Studio来开发: APP已经完善了 ...