您好,登錄后才能下訂單哦!
最近項目中要用到html5來實現,涉及到OC調用JS,以及JS調用OC的方法,這里把遇到的問題以及實現方法介紹一下。
//
// 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(@"開始結束");
// 對于調用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調用JS中的無參方法");
var e = document.getElementById("js_shouw_text");
e.options.add(new Option("OC調用JS中的無參方法", 2));
}
function ocCallJsHasParamsFunction(name, url)
{
alert(name+"的博客地址為:"+url);
var e = document.getElementById("js_shouw_text");
e.options.add(new Option("OC調用JS中的有參方法", 2));
}
[html] view plain copy
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>OC與JS互相調用</title>
</head>
<body>
<div >
<select id="js_shouw_text">
<option>
展示OC調用JS無參數
</option>
</select>
</div>
<div>
<BR/>
<input type="button" value="JS調用OC方法" onclick="js_call_oc()"/>
</div>
<!-- 這里要清楚,雖然test.js跟index.html不同及目錄,實際安裝到程序里面后,是在同級目錄的,所以這里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);
// 發起請求后這個iFrame就沒用了,所以把它從dom上移除掉
iFrame.parentNode.removeChild(iFrame);
iFrame = null;
}
</script>
</body>
</html>
規避1:對于OC去調用JS內容最好在webViewDidFinishLoad方法里或者之后
規避2:在html里面引用js或者css的時候src不要帶有路徑,因為安裝后文件都在同級目錄下面
規避3:OC調用JS的規范
[objc] view plain copy
NSString *js = [NSString stringWithFormat:@"ocCallJsHasParamsFunction('%@','%@');",@"jwzhangjie",@"http://jwzhangjie.cn"];
[self.mWebView stringByEvaluatingJavaScriptFromString:js];
規避4:JS調用OC,這里通過html里面發送一個請求,然后在ios中使用shouldStartLoadWithRequest攔截請求,根據請求url的不同進行處理。
[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);
// 發起請求后這個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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。