Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我
所有文章 外链

Linking

Capturing Life & Tech

  • 主页
  • 随笔
  • 关于我

JavaScriptCore使用

阅读数:次 2017-06-01
字数统计: 420字   |   阅读时长≈ 2分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

//直接执行js代码
- (void)evaluateScript {
//定义一个js并执行函数
JSValue *exeFunction1 = [self.jsContext evaluateScript:@"function hi(){ return 'hi' }; hi()"];
//执行一个闭包js
JSValue *exeFunction2 = [self.jsContext evaluateScript:@"(function(){ return 'hi' })()"];

//更多的应用场景使用网络或者本地文件加载一段js代码,充分利用其灵活性
NSString * path = [[NSBundle mainBundle] pathForResource:@"core" ofType:@"js"];
NSString * html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
JSValue *constructor = [self.jsContext evaluateScript:html];
}

//Native注册方法给js调用
- (void)regiestJSFunction {
//注册一个函数
[self.jsContext evaluateScript:@"var hello = function(){ return 'hello' }"];
//调用
JSValue *value1 = [self.jsContext evaluateScript:@"hello()"];

//注册一个匿名函数
JSValue *jsFunction = [self.jsContext evaluateScript:@" (function(){ return 'hello objc' })"];
//调用
JSValue *value2 = [jsFunction callWithArguments:nil];
}

//注册js方法给Native调用
- (void)regiestNativeFunction {
//注册一个objc方法给js调用
self.jsContext[@"log"] = ^(NSString *msg){
NSLog(@"js:msg:%@",msg);
};
//另一种方式,利用currentArguments获取参数
self.jsContext[@"log"] = ^() {
NSArray *args = [JSContext currentArguments];
for (id obj in args) { NSLog(@"%@",obj); }
};
//使用js调用objc
[self.jsContext evaluateScript:@"native_log('hello,i am js side')"];
}

//注册js错误处理
- (void)jsExceptionHandler {
self.jsContext.exceptionHandler = ^(JSContext *con, JSValue *exception) {
NSLog(@"%@", exception);
con.exception = exception;
};
}

//JSExprot协议使用
- (void)useJSExprot {
Person *p = [[Person alloc]init];
self.jsContext[@"person"] = p;

JSValue *value = [self.jsContext evaluateScript:@"person.whatYouName()"];

}

Person.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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


@protocol JSPersonProtocol <JSExport>

@property (nonatomic, copy) NSDictionary *data;
- (NSString *)whatYouName;

@end


@interface Person : NSObject<JSPersonProtocol>

@property (nonatomic, copy)NSString *name;
- (NSString *)whatYouName;

@end


Person.m

1
2
3
4
5
6
7
8
9
10
#import "Person.h"

@implementation Person

-(NSString *)whatYouName {
return @"hi";
}

@end

//http://blog.iderzheng.com/ios7-objects-management-in-javascriptcore-framework/
//http://www.webryan.net/2013/10/about-ios7-javascriptcore-framework/

  • 本文作者: Linking
  • 本文链接: https://linking.fun/2017/06/01/JavaScriptCore使用的副本/
  • 版权声明: 版权所有,转载请注明出处!
  • JavaScriptCore
  • iOS

扫一扫,分享到微信

摘抄-把时间当朋友
iOS中bundle的使用
目录,不存在的…
© 2015-2026 Linking
GitHub:hexo-theme-yilia-plus by Litten
本站总访问量次 | 本站访客数人
  • 所有文章
  • 外链

tag:

  • weather
  • 需求
  • essay
  • basketball
  • olympic
  • nginx
  • APPScan
  • SQl盲注
  • xss
  • Ajax
  • ajax
  • ai
  • agent
  • openclaw
  • ccf
  • Nginx
  • HTML5
  • html5
  • hmtl5
  • sse
  • JavaScriptCore
  • Oracle
  • operation
  • Linux
  • deploy
  • Mac Office
  • markdown
  • ListView
  • GridView
  • MySQL
  • 慢查询
  • mongodb
  • 转置
  • thought
  • network
  • ubuntu
  • NetworkManager
  • RFKill
  • Netplan
  • avatar
  • cocoa
  • blog
  • Gitalk
  • container
  • macvlan
  • docker
  • oracle
  • cookie
  • patch
  • gitea
  • git
  • iOS
  • https
  • 多线程
  • bundle
  • 兼容性
  • HTTP
  • 绘图
  • cs
  • java
  • 效率
  • 快捷键
  • route
  • nodejs
  • pip
  • arcgis
  • arcgis 建模
  • 标识
  • redis
  • read
  • bookList
  • running
  • showdoc
  • disk
  • unit-test
  • D.Wade
  • thoughts
  • duoduo
  • Python
  • python
  • tomcat
  • 读书节
  • session
  • jdk
  • war
  • 加班
  • Android onclick事件监听
  • 正则
  • 手机品牌匹配
  • ntp
  • OpenLayers
  • Geoserver
  • wechat
  • 微信公众号
  • 爬虫
  • WeChat
  • 张靓颖
  • 动漫
  • vpn
  • PPT
  • MarkDown
  • plan
  • 朱赟
  • 极客时间专栏
  • 极客邦
  • 模块化
  • MVC
  • excel
  • NBA
  • kobe
  • team
  • crawler
  • 进度条
  • ssl
  • book
  • anti-stealing-link
  • Agentic Engineering
  • Vibe Coding
  • Software 3.0
  • Andrej Karpathy
  • LLM
  • Programming

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia-plus根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • GitHub Trending
  • OpenAI ChatGPT
  • Gitee码云
  • 简书
  • CSDN