最近項目中要用到html5來實(shí)現(xiàn),涉及到OC調(diào)用JS,以及JS調(diào)用OC的方法,這里把遇到的問題以及實(shí)現(xiàn)方法介紹一下。

創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、河池網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為河池等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
//
// ViewController.h
// OC_And_JS
//
// Created by 張杰 on 15/7/9.
// Copyright 2015年 張杰. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *oc_call_js_no_params;
@property (weak, nonatomic) IBOutlet UIButton *oc_call_js_has_params;
@property (weak, nonatomic) IBOutlet UIWebView *mWebView;
@property (weak, nonatomic) IBOutlet UILabel *js_call_oc_show;
- (IBAction)ocCallJsNoParams:(id)sender;
- (IBAction)ocCallJsHasParams:(id)sender;
@end
[objc] view plain copy
//
// ViewController.m
// OC_And_JS
//
// Created by 張杰 on 15/7/9.
// Copyright 2015年 張杰. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_mWebView.delegate = self;
//打開URL
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
[self.mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *urlstr = request.URL.absoluteString;
NSRange range = [urlstr rangeOfString:@"ios://jwzhangjie"];
if (range.length!=0) {
_js_call_oc_show.text = [NSString stringWithFormat:@"請訪問地址:%@", urlstr];
}
return YES;
}
-(void)webView:(nonnull UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
NSLog(@"加載失敗");
}
-(void)webViewDidStartLoad:(nonnull UIWebView *)webView{
NSLog(@"開始加載");
}
-(void)webViewDidFinishLoad:(nonnull UIWebView *)webView{
NSLog(@"開始結(jié)束");
// 對于調(diào)用js的時候最好這個方法里面或者之后
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)ocCallJsNoParams:(id)sender {
NSString *js = [NSString stringWithFormat:@"ocCallJsNoParamsFunction();"];
[self.mWebView stringByEvaluatingJavaScriptFromString:js];
}
- (IBAction)ocCallJsHasParams:(id)sender {
NSString *js = [NSString stringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"];
[self.mWebView stringByEvaluatingJavaScriptFromString:js];
}
@end
[javascript] view plain copy
function ocCallJsNoParamsFunction()
{
alert("OC調(diào)用JS中的無參方法");
var e = document.getElementById("js_shouw_text");
e.options.add(new Option("OC調(diào)用JS中的無參方法", 2));
}
function ocCallJsHasParamsFunction(name, url)
{
alert(name+"的博客地址為:"+url);
var e = document.getElementById("js_shouw_text");
e.options.add(new Option("OC調(diào)用JS中的有參方法", 2));
}
[html] view plain copy
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>OC與JS互相調(diào)用</title>
</head>
<body>
<div >
<select id="js_shouw_text">
<option>
展示OC調(diào)用JS無參數(shù)
</option>
</select>
</div>
<div>
<BR/>
<input type="button" value="JS調(diào)用OC方法" onclick="js_call_oc()"/>
</div>
<!-- 這里要清楚,雖然test.js跟index.html不同及目錄,實(shí)際安裝到程序里面后,是在同級目錄的,所以這里src不能加目錄,同樣css也是一樣的 -->
<script type="text/javascript" src="test.js" charset="UTF-8"></script>
<script type="text/javascript">
function js_call_oc()
{
var iFrame;
iFrame = document.createElement("iframe");
iFrame.setAttribute("src", "ios://jwzhangjie");
iFrame.setAttribute("style", "display:none;");
iFrame.setAttribute("height", "0px");
iFrame.setAttribute("width", "0px");
iFrame.setAttribute("frameborder", "0");
document.body.appendChild(iFrame);
// 發(fā)起請求后這個iFrame就沒用了,所以把它從dom上移除掉
iFrame.parentNode.removeChild(iFrame);
iFrame = null;
}
</script>
</body>
</html>
規(guī)避1:對于OC去調(diào)用JS內(nèi)容最好在webViewDidFinishLoad方法里或者之后
規(guī)避2:在html里面引用js或者css的時候src不要帶有路徑,因?yàn)榘惭b后文件都在同級目錄下面
規(guī)避3:OC調(diào)用JS的規(guī)范
[objc] view plain copy
NSString *js = [NSString stringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"];
[self.mWebView stringByEvaluatingJavaScriptFromString:js];
規(guī)避4:JS調(diào)用OC,這里通過html里面發(fā)送一個請求,然后在ios中使用shouldStartLoadWithRequest攔截請求,根據(jù)請求url的不同進(jìn)行處理。
[javascript] view plain copy
function js_call_oc()
{
var iFrame;
iFrame = document.createElement("iframe");
iFrame.setAttribute("src", "ios://jwzhangjie");
iFrame.setAttribute("style", "display:none;");
iFrame.setAttribute("height", "0px");
iFrame.setAttribute("width", "0px");
iFrame.setAttribute("frameborder", "0");
document.body.appendChild(iFrame);
// 發(fā)起請求后這個iFrame就沒用了,所以把它從dom上移除掉
iFrame.parentNode.removeChild(iFrame);
iFrame = null;
}
[objc] view plain copy
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *urlstr = request.URL.absoluteString;
NSRange range = [urlstr rangeOfString:@"ios://jwzhangjie"];
if (range.length!=0) {
_js_call_oc_show.text = [NSString stringWithFormat:@"請訪問地址:%@", urlstr];
}
return YES;
}
文章題目:OC與JS互相調(diào)用
標(biāo)題鏈接:http://www.chinadenli.net/article46/piipeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、全網(wǎng)營銷推廣、網(wǎng)站維護(hù)、外貿(mào)建站、用戶體驗(yàn)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)