TGJSBridge使用

简介:

1、在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

2、在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(0, 0, 320, 480)];

    webView.layer.borderWidth = 2;

    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(100, 300, 100,20 )];

    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:1];

    [dic setValue:@"22" 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 = 0;

    function process()

    {

       alert(1);

        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

相关文章
|
移动开发 缓存 前端开发
客户端内H5页面的首屏性能优化
客户端内H5页面的首屏性能优化
442 0
客户端内H5页面的首屏性能优化
|
移动开发 JavaScript Android开发
H5页面卡顿加载慢原因分析
H5页面卡顿加载慢原因分析
785 0
|
7天前
|
NoSQL Cloud Native Redis
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
阿里云瑶池数据库团队后续将持续参与Valkey社区,如过往在Redis社区一样耕耘,为开源社区作出持续贡献。
Redis核心开发者的新征程:阿里云与Valkey社区的技术融合与创新
|
7天前
|
关系型数据库 分布式数据库 数据库
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
PolarDB分布式版助力《香肠派对》实现百亿好友关系20万QPS的毫秒级查询。
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
|
8天前
|
消息中间件 Cloud Native Serverless
RocketMQ 事件驱动:云时代的事件驱动有啥不同?
本文深入探讨了云时代 EDA 的新内涵及它在云时代再次流行的主要驱动力,包括技术驱动力和商业驱动力,随后重点介绍了 RocketMQ 5.0 推出的子产品 EventBridge,并通过几个云时代事件驱动的典型案例,进一步叙述了云时代事件驱动的常见场景和最佳实践。
115060 1
|
9天前
|
弹性计算 安全 API
访问控制(RAM)|云上安全使用AccessKey的最佳实践
集中管控AK/SK的生命周期,可以极大降低AK/SK管理和使用成本,同时通过加密和轮转的方式,保证AK/SK的安全使用,本次分享为您介绍产品原理,以及具体的使用步骤。
101833 3
|
8天前
|
自然语言处理 Cloud Native Serverless
通义灵码牵手阿里云函数计算 FC ,打造智能编码新体验
近日,通义灵码正式进驻函数计算 FC WebIDE,让使用函数计算产品的开发者在其熟悉的云端集成开发环境中,无需再次登录即可使用通义灵码的智能编程能力,实现开发效率与代码质量的双重提升。
95408 2
|
5天前
|
物联网 PyTorch 测试技术
手把手教你捏一个自己的Agent
Modelscope AgentFabric是一个基于ModelScope-Agent的交互式智能体应用,用于方便地创建针对各种现实应用量身定制智能体,目前已经在生产级别落地。
|
1天前
|
NoSQL Java Redis
使用Redis实例搭建网上商城的商品相关性分析程序
本教程将指导您如何快速创建实例并搭建网上商城的商品相关性分析程序。(ApsaraDB for Redis)是兼容开源Redis协议标准的数据库服务,基于双机热备架构及集群架构,可满足高吞吐、低延迟及弹性变配等业务需求。
17082 0
Doodle Jump — 使用Flutter&Flame开发游戏真不错!
用Flutter&Flame开发游戏是一种什么体验?最近网上冲浪的时候,我偶然发现了一个国外的游戏网站,类似于国内的4399。在浏览时,我遇到了一款经典的小游戏:Doodle Jump...
112745 12

热门文章

最新文章