Html中用js回调OC方法
来源:互联网 发布:诸神黄昏灵羽进阶数据 编辑:程序博客网 时间:2024/05/16 00:31
朋友是做安卓的,她在html中成功回调了安卓的方法,问我ios怎么回调。这个东东没做过啊,赶紧问度娘,原来要用到JavaScriptCore.framework.
啥都不说了,先贴代码
ViewController.h
#import <UIKit/UIKit.h>#import <JavaScriptCore/JavaScriptCore.h>@interface ViewController : UIViewController@property (strong, nonatomic) IBOutlet UIWebView *webView;@end
ViewController.m
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]]; JSContext *context = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; context[@"callOC"] = ^() { NSLog(@"+++++++callOC+++++++"); };}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
index.html
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,user-scalable=no" /> <title>HTML中用JS调用OC方法</title> <script type="text/javascript" language="javascript"> function onClick() { if (navigator.userAgent.indexOf('AppleWebKit') >= 0) { callOC(); return; } //其他处理 } </script> </head> <body> <input type="button" value="求求你点我吧" onclick="onClick()" /> </body></html>
0 0
- Html中用js回调OC方法
- UIWebView中Html中用JS调用OC方法及OC执行JS代码
- UIWebView中Html中用JS调用OC方法及OC执行JS代码
- UIWebView中Html中用JS调用OC方法及OC执行JS代码
- UIWebView中Html中用JS调用OC方法及OC执行JS代码
- UIWebView中Html中用JS调用OC方法及OC执行JS代码
- js、oc、html交互
- oc调用js方法
- HTML中用JS获取GET的参数
- html中用js调用ASP文件
- 在HTML中用JS接收参数
- html中用js动态显示时间总结
- html网页中用js实现替换字符串
- html中用js格式化JSON输出
- 如何在ios工程中用oc调用js算法
- iOS JS传值给OC方法(附HTML代码)
- IOS js调OC方法
- iOS js调用oc方法
- P124第45题
- private static final long serialVersionUID的作用
- 邢台自闭症去哪里治
- 邢台自闭症哪里治
- 网站推荐
- Html中用js回调OC方法
- 邢台自闭症哪里看的好
- docker中两个容器间的互访
- TCP三次握手
- 邢台自闭症哪家治最好
- Android电源管理-休眠简要分析
- ios8之后的CLLOCATION定位权限申请
- 第三章第41题
- C#里partial关键字的作用(转摘)