oc和js交互

代碼里面 一共兩種和js交互的方法注釋的是另一種
點(diǎn)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

點(diǎn)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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、說(shuō)明 這篇文章記錄自己在研究OC與JS交互中的所得,以及遇到的問(wèn)題與解決 由于蘋(píng)果的審核時(shí)間太漫長(zhǎng),一次審核不...
    CoderSC閱讀 1,089評(píng)論 0 5
  • 前言 在iOS開(kāi)發(fā)過(guò)程中,一般會(huì)有遇到需要和UIWebView交互的需求,即native端和網(wǎng)頁(yè)端的數(shù)據(jù)交互,因?yàn)?..
    wuwy閱讀 986評(píng)論 0 2
  • 作為一名合格的iOS開(kāi)發(fā), 光了解Native是不夠的, 在很多情況下, 我們都要和Web去做交互, 了解OC和W...
    bigParis閱讀 1,111評(píng)論 0 2
  • OC和JS交互有三種方式:UIWebView(WKWebView)、JavaScriptCore、WebViewJ...
    第1001號(hào)群眾演員閱讀 617評(píng)論 0 3
  • 上回書(shū)說(shuō) OC和JS交互的一些準(zhǔn)備工作, 下面開(kāi)始OC和JS交互的重頭戲->JS調(diào)用OC. 這里我們會(huì)用到Safa...
    bigParis閱讀 568評(píng)論 0 1

友情鏈接更多精彩內(nèi)容