、在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. PostgreSQL 数据类型

    数值类型 数值类型由两个字节,4字节和8字节的整数,4字节和8字节的浮点数和可选精度的小数.下表列出了可用的类型. www.yiibai.com Name Storage Size Descripti ...

  2. C语言第四讲,typedef 关键字,以及作用域

    C语言第四讲,typedef 关键字,以及作用域 一丶typedef关键字 在C语言中,有typedef 关键字,这个关键字的作用就是允许你为类型定义一个新的名字,也就是 起个别的名字 例如: typ ...

  3. BBWebImage 设计思路

    BBWebImage 设计思路 BBWebImage 是高性能 Swift 图片组件,用于图片下载.缓存.编解码.编辑与展示. GitHub 地址: https://github.com/Silenc ...

  4. openssl签署和自签署证书的多种实现方式

    openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.采用自定义配置文件的实现方法 1.1 自建CA 自建CA的机制:1.生成 ...

  5. Vim 多行剪切、复制和删除

    剪切 快捷键方式: dd:剪切光标所处当前行 n + dd:剪切光标所在行及以下共 n 行 按 p 粘贴在光标所在行 命令行方式: 例如剪切1到10行,并粘贴在12行处: 1,10 m 12 复制 快 ...

  6. LeetCode 任务调度器-Python3<八>

    题目:https://leetcode-cn.com/problems/task-scheduler/description/ 给定一个用字符数组表示的 CPU 需要执行的任务列表.其中包含使用大写的 ...

  7. Android开发——adb连接夜神模拟器

    夜神模拟器安装完之后,打开模拟器,在cmd命令行中进入到夜神模拟器的bin目录 本来以为不支持中文的,没想到确实支持的哈哈 之后输入adb connect 就能成功连接上了 PS:在Android S ...

  8. 【Java并发编程】21、线程池ThreadPoolExecutor源码解析

    一.前言 JUC这部分还有线程池这一块没有分析,需要抓紧时间分析,下面开始ThreadPoolExecutor,其是线程池的基础,分析完了这个类会简化之后的分析,线程池可以解决两个不同问题:由于减少了 ...

  9. React中使用百度地图API

    今天我们来搞一搞如何在React中使用百度地图API好吧,最近忙的头皮发麻,感觉身体被掏空,所以很久都没来写博客了,但今天我一定要来一篇好吧 话不多说,我们直接开始好吧 特别注意:该React项目是用 ...

  10. VUE路由转场特效,WebAPP的前进与后退

    一.效果图 二.思路 1. 定义两个 CSS 过度动画,前进与后退: slide-right-enter   和   slide-left-enter 2. 给路由配置meta信息,设置各个路由的级别 ...