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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// CustomWKWebViewController.m
// Lighting
//
// Created by 曹云霄 on 16/5/17.
// Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//
#import "CustomWKWebViewController.h"
#import <WebKit/WebKit.h>
@interface CustomWKWebViewController ()<WKNavigationDelegate,WKScriptMessageHandler>
@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,strong) WKWebViewConfiguration *config;
@end
@implementation CustomWKWebViewController
#pragma mark - lazy
- (WKWebViewConfiguration *)config
{
if (!_config) {
_config = [[WKWebViewConfiguration alloc]init];
// 设置偏好设置
_config.preferences = [[WKPreferences alloc] init];
// 默认为0
_config.preferences.minimumFontSize = 10;
// 默认认为YES
_config.preferences.javaScriptEnabled = YES;
// 在iOS上默认为NO,表示不能自动通过窗口打开
_config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
// 通过JS与webview内容交互
_config.userContentController = [[WKUserContentController alloc] init];
[_config.userContentController addScriptMessageHandler:self name:@"AppModel"];
}
return _config;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self uiConfigAction];
[self addDismissButton];
}
#pragma mark -UI
- (void)uiConfigAction
{
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:self.config];
[self.view addSubview:self.webView];
self.webView.navigationDelegate = self;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.pdfURLString]]];
}
#pragma mark - 添加删除按钮
- (void)addDismissButton
{
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:dismissButton];
[dismissButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(40);
make.top.mas_equalTo(40);
make.size.mas_equalTo(CGSizeMake(50, 50));
}];
[dismissButton setBackgroundImage:TCImage(@"1") forState:UIControlStateNormal];
[dismissButton addTarget:self action:@selector(dismissButtonClick) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
if ([message.name isEqualToString:@"AppModel"]) {
// 打印所传过来的参数,只支持NSNumber, NSString, NSDate, NSArray,
// NSDictionary, and NSNull类型
if (self.luckyDrawFinishBlock) {
self.luckyDrawFinishBlock(message.body);
}
}
}
#pragma mark -dismiss
- (void)dismissButtonClick
{
WS(weakSelf);
[self dismissViewControllerAnimated:YES completion:^{
if (weakSelf.dismissLuckyDrawController) {
weakSelf.dismissLuckyDrawController();
}
}];
}
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{
[self CreateMBProgressHUDLoding];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
[self RemoveMBProgressHUDLoding];
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
[self ErrorMBProgressView:@"加载失败"];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end