zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    《银光志Silverlight 3.0开发详解与最佳实践》出版电子版——风云编著
    Nigel Parker 40分钟视频演示了微软的 31 项技术(附下载)
    《银光志Silverlight 3.0开发详解与最佳实践》书搞目录
    Silverlight 2使用C#遍历XML(兼容Silverlight3)
    为什么要把Silverlight归入Web 2.0?
    Silverlight明年将占据互联网设备半壁江山
    Silverlight Tools 3.0中文正式版发布(附下载地址)
    银客帝国招聘Silverlight兼职开发人员
    再说招聘:学开车一定要摸方向盘
    Expression Blend 4 下载
  • 原文地址:https://www.cnblogs.com/zhanggui/p/3917777.html
Copyright © 2011-2022 走看看