您的当前位置:首页正文

iOS js与oc交互(js调用oc篇)

来源:花图问答

从iOS7开始 苹果公布了JavaScriptCore.framework 它使得JS与OC的交互更加方便了。
首先导入 #import <JavaScriptCore/JavaScriptCore.h>

方法一:

js代码:(写一个简单的h5的一个按钮并绑定事件test())

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>

        function load(){
            document.getElementById("target").onclick();
        }
        function test(){
          // js执行的代码
          
        }
    </script>
</head>
<body onload="load()">

<button id="target" onclick="test()">测试这个按钮的点击</button>
</body>
</html>

oc代码

#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface ViewController () <UIWebViewDelegate, JSExport>

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.delegate = self;
    webView.backgroundColor = [UIColor redColor];
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"js" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlString baseURL:nil];
    
    [self.view addSubview:webView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// 获取webView上的js
    JSContext *contest = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    contest[@"test"] = ^(){
        NSLog(@"test按钮被点击了!!");
        // 这里网页上的按钮被点击了, 客户端可以在这里拦截到,并进行操作
    };
}

方法二:

我是使用这个方法做的, 后台需要和安卓,iOS共同配合, 点击webView上的success按钮, 让后台调用app的方法

js代码(定义一个接收的类)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>

        function load(){

            document.getElementById("target").onclick();
        }

        function test(){
            
        // 后台可以这样写, 调用oc中IOS这个类的print('123')方法
        // 可以传递参数, 如果不想传递参数,则js和oc均不写参数即可
            IOS.print('123');
            
        }

    </script>
</head>
<body onload="load()">

<button id="target" onclick="test()">测试这个按钮的点击</button>
</body>
</html>

oc代码(定义一个接收的类)

IOS.h
#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>

@protocol JSObjextProtocol <JSExport>

// 如果后台没有传递参数, 这里不填写参数
- (void)print:(NSString *)jsonString;

@end


@interface IOS : NSObject <JSObjextProtocol>

@end


IOS.m
#import "IOS.h"

@implementation IOS
- (void)print:(NSString *)jsonString
{
    NSLog(@"=============%@", jsonString);
}

@end
ViewController.m
#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "IOS.h"
@interface ViewController () <UIWebViewDelegate, JSExport>

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.delegate = self;
    webView.backgroundColor = [UIColor redColor];
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"js" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlString baseURL:nil];
    
    [self.view addSubview:webView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    JSContext *contest = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//    contest[@"test"] = ^(){
//        NSLog(@"__________test");
//        
//    };
    IOS *ios=[IOS new];
    contest[@"IOS"]=ios;
}