使用JavaScriptCore實現(xiàn)OC和JS交互詳解
JavaScriptCore
JavaScriptCore是webkit的一個重要組成部分,主要是對JS進(jìn)行解析和提供執(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種情況
- JS調(diào)用OC
- JS調(diào)用OC并傳遞參數(shù)
- OC調(diào)用JS
- OC調(diào)用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調(diào)用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調(diào)用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里面,其實就是轉(zhuǎn)換成了JS中命名為callOC的function。然后我們直接執(zhí)行這個function,調(diào)用的就是我們的block里面的內(nèi)容了。
傳過來的參數(shù)可以通過[JSContext currentArguments]這個array接受,里面是JSValue對象。
OC調(diào)用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調(diào)用JS。
demo已上傳,需要的可以點此下載查看。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
使用JS實現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能
這篇文章主要介紹了使用JS實現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能,需要的朋友可以參考下2014-10-10
關(guān)于javascript 回調(diào)函數(shù)中變量作用域的討論
關(guān)于回調(diào)函數(shù)中變量作用域的討論精品推薦,大家可以參考下。2009-09-09
前端開發(fā)基礎(chǔ)javaScript的六大作用
這篇文章主要介紹了前端開發(fā)基礎(chǔ)javaScript的六大作用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

