oc和js交互

代码里面 一共两种和js交互的方法注释的是另一种
点h文件

//
//  ELE_testViewController.h
//  ELE_Information
//
//  Created by chengl on 16/6/7.
//  Copyright © 2016年 eleme. All rights reserved.
//

#import "ELE_baseVIewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@protocol TestJSExport <JSExport>
-(void)close;
-(void)hideBar:(NSString *)str;
-(void)showBar:(NSString *)str;


@end
@interface ELE_testViewController : ELE_baseVIewController<TestJSExport>

@property (strong, nonatomic) JSContext *context;

@property (strong, nonatomic)UIWebView *webView;

@property (nonatomic,strong)NSString *urlStr;
@end

点m文件

//
//  ELE_testViewController.m
//  ELE_Information
//
//  Created by chengl on 16/6/7.
//  Copyright © 2016年 eleme. All rights reserved.
//

#import "ELE_testViewController.h"

@interface ELE_testViewController ()

@end

@implementation ELE_testViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSURL *url=[[NSBundle mainBundle]URLForResource:@"obj" withExtension:@"html"];
    NSMutableURLRequest *tmpRequest=[[NSMutableURLRequest alloc] initWithURL:url];
    [self.webView loadRequest:tmpRequest];



    
}

-(UIWebView*)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,SCREEN_HEIGHT)];
        _webView.delegate = (id)self;
        _webView.scalesPageToFit = YES;
        _webView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_webView];

    }
    return _webView;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    

    self.context =[self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息:%@", exceptionValue);
    };
    self.context[@"App"] = self ;
//    __weak typeof(self) weakSelf = self;
//    self.context[@"showBar"] = ^() {
//        __strong typeof(self) strongSelf = weakSelf;
//        if (strongSelf)
//        {
//            NSLog(@"123");
//        }
//        else
//        {
//            return;
//        }
//    };
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)hideBar:(NSString *)str
{
    NSLog(@"00000");
}
-(void)showBar:(NSString *)str
{
    NSLog(@"00000");
}
-(void)close
{
    
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、说明 这篇文章记录自己在研究OC与JS交互中的所得,以及遇到的问题与解决 由于苹果的审核时间太漫长,一次审核不...
    CoderSC阅读 4,457评论 0 5
  • 前言 在iOS开发过程中,一般会有遇到需要和UIWebView交互的需求,即native端和网页端的数据交互,因为...
    wuwy阅读 4,427评论 0 2
  • 作为一名合格的iOS开发, 光了解Native是不够的, 在很多情况下, 我们都要和Web去做交互, 了解OC和W...
    bigParis阅读 4,626评论 0 2
  • OC和JS交互有三种方式:UIWebView(WKWebView)、JavaScriptCore、WebViewJ...
    第1001号群众演员阅读 3,449评论 0 3
  • 上回书说 OC和JS交互的一些准备工作, 下面开始OC和JS交互的重头戏->JS调用OC. 这里我们会用到Safa...
    bigParis阅读 3,359评论 0 1