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使用的更多相关文章
随机推荐
- Java中锁分类
锁的分类大致如下:公平锁/非公平锁可重入锁/不可重入锁独享锁/共享锁乐观锁/悲观锁分段锁 1.公平锁/非公平锁公平锁就是严格按照线程启动的顺序来执行的,不允许其他线程插队执行的:而非公平锁是允许插队的 ...
- 《剑指offer》内容总结
(1)剑指Offer——Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是统计和排序大量的字符串(但不仅限于字符串),所以经常 ...
- TopK
网易面试挂了,伤心. 一面面试官不是搞技术的,二面面试官搞ios,全程不问JVM,并发的知识,运气真差 而且手撸代码硬伤,没得编译 准备先在IDE敲一遍,在再纸上面写一遍. package com.q ...
- python中逻辑运算符“+”的特殊之处
num = num + num 与 num += num 的区别(其他语言中这两种方式可以划等号,但是python中不可以): num = num + num: num = [100] def tes ...
- 深入浅出 JVM ClassLoader
# 前言 在 JVM 综述里面,我们说,JVM 做了三件事情,Java 程序的内存管理, Java Class 二进制字节流的加载(ClassLoader),Java 程序的执行(执行引擎).我们也说 ...
- MailBee.NET
MailBee.NET Objects 是一款为创建.发送.接收以及处理电子邮件而设计的健壮.功能丰富的.NET控件.具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加 ...
- 无法初始化 PowerShell 主机解决方案
Question无法初始化 PowerShell 主机.如果您的 PowerShell 执行策略设置设为 AllSigned,请先打开程序包管理器控制台以初始化该主机. --------------- ...
- asp.net mvc之自定义WebViewPage
采用Razor引擎的View文件最终都会编译成一个WebViewPage类型, 通过自定义WebViewPage,添加相应的属性和方法,你可以很方便的在View里调用, 自定义WebViewPage只 ...
- 【开发工具之Spring Tool Suite】6、用Spring Tool Suite简化你的开发
如果你是一个喜欢用spring的人,你可能会在欣赏spring的强大功能外,对其各样的配置比较郁闷,尤其是相差较大的版本在配置文件方面会存在差异,当然你可以去花不少的时间去网上查找相关的资料,当你准备 ...
- java_二进制的前导的零
题目内容: 计算机内部用二进制来表达所有的值.一个十进制的数字,比如18,在一个32位的计算机内部被表达为00000000000000000000000000011000.可以看到,从左边数过来,在第 ...