上代碼膨报。
#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#define RandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0f];
@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic,copy) NSString * htmlStr;
@end
@implementation ViewController
#pragma mark -- 懶加載
- (NSString *)htmlStr
{
if (_htmlStr == nil) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];
NSString * htmlStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
_htmlStr = htmlStr;
}
return _htmlStr;
}
#pragma mark -- 生命周期
- (void)viewDidLoad{
[super viewDidLoad];
NSLog(@"html:%@",self.htmlStr);
self.webView.delegate = self;//設(shè)置代理
[self.webView loadHTMLString:self.htmlStr baseURL:nil];//加載頁面
}
#pragma -- webView delegate
//注意一點(diǎn)。這里向頁面注入js方法耕肩,建議在webview加載完頁面之后的代理里面進(jìn)行民假。
//確痹纬牵可以獲取html的js上下文逞泄。以便將方法插入到j(luò)s上下文中
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"網(wǎng)頁加載完畢");
//獲取到j(luò)s的上下文
JSContext * context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//注入方法
context[@"hello"]= ^(){
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"點(diǎn)擊了 hello" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
};
context[@"sure"]= ^(){
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"點(diǎn)擊了 sure" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
};
context[@"change"]= ^(){
self.webView.backgroundColor = RandomColor;
};
}
@end
HTML代碼
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- JQuery -->
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="${pageContext.request.contextPath }/style/news.css">
<title>Insert title here</title>
</head>
<body>
<div class="container">
<button onclick="hello()" type="button" class="btn btn-primary btn-lg btn-block">hello button</button>
<button onclick="sure()" type="button" class="btn btn-default btn-lg btn-block">sure button</button>
<button onclick="change()" type="button" class="btn btn-default btn-lg btn-block">change button</button>
<div>
</body>
</html>