欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

如何使用JavaScriptCore實現(xiàn)OC和JS交互

這篇文章主要介紹了如何使用JavaScriptCore實現(xiàn)OC和JS交互,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)公司公司2013年成立,先為寧夏等服務建站,寧夏等地企業(yè),進行企業(yè)商務咨詢服務。為寧夏企業(yè)網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。

JavaScriptCore

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

首先創(chuàng)建webView,讀取本地的html文件

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

在demo中,我們要實現(xiàn)4種情況

  1. JS調用OC

  2. JS調用OC并傳遞參數(shù)

  3. OC調用JS

  4. OC調用JS并傳遞參數(shù)

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('參數(shù)1 ','參數(shù)2 ','參數(shù)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。然后我們直接執(zhí)行這個function,調用的就是我們的block里面的內容了。

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

OC調用JS

初始化兩個Button,在點擊事件中實現(xiàn)如下方法

- (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"]];
}

即可實現(xiàn)OC調用JS。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用JavaScriptCore實現(xiàn)OC和JS交互”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!

新聞名稱:如何使用JavaScriptCore實現(xiàn)OC和JS交互
文章源于:http://aaarwkj.com/article46/iipgeg.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、自適應網站微信公眾號、移動網站建設、小程序開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網站建設
国产综合一区二区三区视频| 中字幕人妻一区二区三区| 亚洲国产欧美日韩在线| 成年女人毛片免费观看不卡| 色久悠悠婷婷综合在线亚洲| 免费不卡无码毛片观看| 日韩x级av免费在线观看| 久久国产精品一区二区三区 | 日本在线看片一区二区| 欧美亚洲精品一区二区三区| 国内成人免费在线视频| 精彩国产av一区二区三区| 日韩在线中文字幕精品| 成人深夜福利视频观看| 色婷婷国产精品久久包臀| 国产大片在线观看一区二区| 色哟国产传媒视频在线观看| 国产精品日韩av一区二区| 亚洲伦理在线一区二区| 无人视频在线观看高清91| 欧美日本国产高清不卡| 日韩成人中文字幕电影| 尤物在线免费观看视频| 日韩在线观看精品亚洲| 中文日韩av在线免费播放| 日本东京热加勒比视频| 久久五月婷婷爱综合亚洲| 黑人巨大精品欧美黑寡妇| 午夜在线免费观看小视频| 三级黄色片免费久久久| 调教亚洲另类唯美第二页| 欧美午夜激情片在线观看| 久碰精品少妇中文字幕av| 九九在线视频免费观看精品视频| 久久九特黄的免费大片| 亚洲伊人久久一区二区| 久久久国产一区二区三区| 中文字幕乱码熟女人妻视频| 国产日韩欧美一区综合| 福利在线视频一区二区| 国产精品三级av在线播放|