码迷,mamicode.com
首页 > Web开发 > 详细

TGJSBridge使用

时间:2014-08-17 15:31:32      阅读:1436      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   java   使用   os   io   strong   

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使用,布布扣,bubuko.com

TGJSBridge使用

标签:style   http   color   java   使用   os   io   strong   

原文地址:http://www.cnblogs.com/zhanggui/p/3917777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!