、在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. react 字符串强转为html标签

    react中,富文本编辑 从数据库取出来 是带标签的 字符串,需要强转为 节点 <div dangerouslySetInnerHTML={{ __html: this.state.obj.ht ...

  2. Haproxy+Heartbeat 高可用集群方案操作记录

    之前详细介绍了haproxy的基础知识点, 下面记录下Haproxy+Heartbeat高可用web集群方案实现过程, 以加深理解. 架构草图如下: 1) 基本环境准备 (centos6.9系统) 1 ...

  3. NIO ServerSocketChannel ScoketChannel

    package com.yb.nio; import java.io.IOException; import java.net.InetSocketAddress; import java.net.S ...

  4. 进程间通信IPC-管道

    管道是UNIX系统IPC的最古老的形式,所有的UNIX系统都提供此通讯机制.管道有以下两种局限性: 1, 历史上,它们是半双工的(即数据只能在一个方向上流动).现在某些系统提供了全双工管道,但是为了最 ...

  5. k8s小工具

    1.Kubectx kubectx是一个在多集群和多命名空间的时候使用的非常好用的工具,kubectx与kubens绑定,kubectx用来在集群之间切换,kubens用来切换namespace. # ...

  6. 【SQL】小心在循环中声明变量——浅析SQL变量作用域

    本文适用:T-SQL(SQL Server) 先看这个语句: --跑3圈 BEGIN --每圈都定义一个表变量,并插入一行 DECLARE @t TABLE(Col INT PRIMARY KEY) ...

  7. C# if---else---练习题整理

    if    else    语句是到今天为止学习的第一个完整的语句,把有意思的练习题整理下来开一下脑洞!!! 练习一简单的人工智能 1 static void Main(string[] args) ...

  8. php中mysql和mysqli的总结

    首先php—mysql 是 php 操作 mysql 资料库最原始的的拓展 而php—mysqli,字母i代表的 Improvement ,提更了相对进阶的功能. 推荐学习和使用mysqli mysq ...

  9. Bootstrap 、AngularJs

    SPA 全称:single-page application单页面应用 说明:类似原生客户端软件更流畅的用户体验的页面.所有的资源都是按需加载到页面上. JSR 全称:Java Specificati ...

  10. 2018-01-17 Antlr4实现简单语言之整数比较表达式

    续上文Antlr4: 修改语法规则更接近普通BNF格式. 例程 为先=1 为先 为2 => 返回false '为'作为关键词, 与数字可以连写, 但必须与变量名用空格间隔: 变量一=1 变量二= ...