-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewController.m
More file actions
65 lines (49 loc) · 2.27 KB
/
ViewController.m
File metadata and controls
65 lines (49 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// ViewController.m
// JavaScriptBridgeDemo
//
// Created by sue on 2020/6/18.
// Copyright © 2020 sharlockk. All rights reserved.
//
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import "JavaScriptBridge.h"
@interface ViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic, strong) WKWebView *wkWebView;
@property (nonatomic, strong) JavaScriptBridge *bridge;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.wkWebView];
self.bridge = [JavaScriptBridge bridgeForViewController:self webView:self.wkWebView];
// Do any additional setup after loading the view.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString * htmlPath = [[NSBundle mainBundle] pathForResource:@"demo"
ofType:@"html"];
NSString * htmlContent = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[self.wkWebView loadHTMLString:htmlContent baseURL:baseURL];
}
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:^{
}];
if(completionHandler)
completionHandler();
}
- (WKWebView *)wkWebView {
if (!_wkWebView) {
_wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_wkWebView.UIDelegate = self;
_wkWebView.navigationDelegate = self;
_wkWebView.allowsBackForwardNavigationGestures = YES;
}
return _wkWebView;
}
@end