、在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使用的更多相关文章

随机推荐

  1. 一篇极好的Git 总结

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯工蜂发表于云+社区专栏 常用命令 简单的,常用的命令也就几个.但是想非常熟练使用,怕是要记住几十个. Workspace:工作区( ...

  2. SpringBoot----跨域配置

    前言: 当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时,一个资源会发起一个跨域HTTP请求(Cross-site HTTP request).比如说,域名A ( http://dom ...

  3. SpringMVC教程2

    接上篇文章-SpringMVC教程1 五.基本操作 1.响应请求的方式 1.1ModeAndView /** * 查询方法 * @return */ @RequestMapping("/qu ...

  4. 2017ACM/ICPC亚洲区沈阳站(部分解题报告)

    HDU 6225 Little Boxes 题意 计算四个整数的和 解题思路 使用Java大整数 import java.math.BigInteger; import java.util.Scann ...

  5. [Luogu 3787] 冰精冻西瓜

    Description 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有根树,琪露诺想要把它们冷冻起来慢慢吃. 这些西瓜蔓具有神奇的性质, ...

  6. DML和索引内部结构变化

    1.修改数据对索引结构的影响 合适的索引对查询性能和效率的提升是巨大的,但是万事有利有弊,拥有索引的表在增.删.改记录时需要去维护索引.如何让增.删.改更快速更高效?这就需要了解数据修改时对索引结构会 ...

  7. ASOC 音频子系统框架

    基于: Mini2440 开发板, Linux 3.4.2 内核 ASOC 简介: ASoC - ALSA System on Chip,是建立在标准ALSA驱动层上,为了更好地支持嵌入式处理器和移动 ...

  8. ES6+ 开发 React 组件

    在这里简要的说一下这些语言新特性对 React 应用的开发有什么影响,这些 ES6+ 特性使得 React 开发更简单更有趣. 类 迄今为止,最能体现我们使用 ES6+ 来编写 React 组件的就是 ...

  9. Memcached部署和用法

    一.Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...

  10. canvas-2rect.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...