国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

使用JavaScriptCore實現OC和JS交互詳解

2019-11-19 17:00:24
字體:
來源:轉載
供稿:網友

JavaScriptCore

JavaScriptCore是webkit的一個重要組成部分,主要是對JS進行解析和提供執行環境。iOS7后蘋果在iPhone平臺推出,極大的方便了我們對js的操作。

首先創建webView,讀取本地的html文件

 NSURL* htmlURL = [[NSBundle mainBundle] URLForResource: @"demo" withExtension: @"html"];[_webView loadRequest: [NSURLRequest requestWithURL: htmlURL]];

在demo中,我們要實現4種情況

  1. JS調用OC
  2. JS調用OC并傳遞參數
  3. OC調用JS
  4. OC調用JS并傳遞參數

html文件中代碼如下

<html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function showAlert(){  alert('OC call JS with no argument'); } function showAlertWithString(string){  alert(string); } function callOCWithArgument() {  jsCallOCWithArgument('參數1 ','參數2 ','參數3'); } </script></head><body> </br> </br> </br> </br> <form>  <button type='button' onclick='callOC()'>jsCallOC</button>  <button type='button' onclick='callOCWithArgument()'>jsCallOCWithArgument</button> </form></body></html>

JS調用OC

在webView的代理方法webViewDidFinishLoad中

-(void)webViewDidFinishLoad:(UIWebView *)webView{ _context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; __weak typeof(self) weakSelf = self; _context.exceptionHandler = ^(JSContext *context, JSValue *exception) {  weakSelf.context.exception = exception; }; //js調用OC _context[@"callOC"] = ^() {  NSArray *args = [JSContext currentArguments];  for (JSValue *jsVal in args) {   NSLog(@"%@", jsVal.toString);  }  dispatch_async(dispatch_get_main_queue(), ^{   UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"arguments" message:@"JS Call OC With No Argument" preferredStyle:UIAlertControllerStyleAlert];   UIAlertAction * action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {   }];   [alertView addAction:action];   [weakSelf presentViewController:alertView animated:YES completion:nil];  }); }; _context[@"jsCallOCWithArgument"] = ^() {  NSArray *args = [JSContext currentArguments];  NSMutableString * stirng = [NSMutableString string];  for (JSValue * value in args) {   [stirng appendString:value.toString];  }  dispatch_async(dispatch_get_main_queue(), ^{   UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"arguments" message:stirng preferredStyle:UIAlertControllerStyleAlert];   UIAlertAction * action = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {   }];   [alertView addAction:action];   [weakSelf presentViewController:alertView animated:YES completion:nil];  }); }; }

我們定義一個block,然后保存到context里面,其實就是轉換成了JS中命名為callOC的function。然后我們直接執行這個function,調用的就是我們的block里面的內容了。

傳過來的參數可以通過[JSContext currentArguments]這個array接受,里面是JSValue對象。

OC調用JS

初始化兩個Button,在點擊事件中實現如下方法

- (IBAction)callJS:(id)sender { [_context evaluateScript:@"showAlert()"];}- (IBAction)callJSWithArguments:(id)sender { [_context evaluateScript:@"showAlertWithString('OC call JS with arguments')"];// [_context[@"showAlertWithString"] callWithArguments:@[@"OC call JS with arguments"]];}

即可實現OC調用JS。

demo已上傳,需要的可以點此下載查看。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 顺义区| 安丘市| 兴义市| 淅川县| 屏南县| 洱源县| 新源县| 略阳县| 都江堰市| 定安县| 琼海市| 会泽县| 工布江达县| 济南市| 郓城县| 蒙城县| 中西区| 大石桥市| 阿城市| 安仁县| 浑源县| 确山县| 泾源县| 荣成市| 曲水县| 寻甸| 和林格尔县| 无为县| 霍林郭勒市| 丹寨县| 贞丰县| 界首市| 金山区| 太保市| 海宁市| 澄城县| 曲麻莱县| 衡水市| 咸丰县| 紫阳县| 忻城县|