• 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

  • 相关阅读:
    图论复习
    数据结构复习
    数学复习
    字符串算法复习
    还有9天就要走了
    JS 多个一维数组排例组合的所有可能
    PHP 多个一维数组排列组合的所有可能
    维护DOWNID
    LINUX
    Multiple Versions of PHP run on server
  • 原文地址:https://www.cnblogs.com/zhanggui/p/3917777.html
Copyright © 2020-2023  润新知