CNLiveBaseWebViewController.m
4.17 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// CNLiveBaseWebViewController.m
// CNLiveBaseClass
//
// Created by CNLive-zxw on 2019/12/7.
// Copyright © 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveBaseWebViewController.h"
#import <WebKit/WebKit.h>
#import "QMUIKit.h"
#import "CNLiveCategory.h"
#import "CNLiveWebView.h"
#import "CNLiveNavigationBar.h"
@interface CNLiveBaseWebViewController ()<CNLiveWebViewDelegate>
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, assign) BOOL closeHidden;
@end
@implementation CNLiveBaseWebViewController
static const NSInteger margin = 10;
#pragma mark - 初始化
- (instancetype)initWithUrl:(NSString *)url{
return [self initWithUrl:url title:@"" closeHidden:YES];
}
- (instancetype)initWithUrl:(NSString *)url title:(nullable NSString *)title{
return [self initWithUrl:url title:title closeHidden:YES];
}
- (instancetype)initWithUrl:(NSString *)url title:(nullable NSString *)title closeHidden:(BOOL)closeHidden {
self = [super init];
if (self) {
self.url = url;
self.title = title;
self.closeHidden = closeHidden;
}
return self;
}
#pragma mark - Setter方法
- (void)setUrl:(NSString *)url{
if(!url||[url isEqualToString:@""]) return;
_url = url;
}
- (NSString *)editHtml:(NSString *)url{
return [NSString stringWithFormat:@"<div style=\"font-size:45px\">%@</div>",url];
}
#pragma mark - 生命周期
- (void)viewDidLoad {
[super viewDidLoad];
if ([_url hasPrefix:@"http://"]||[_url hasPrefix:@"https://"]) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
[self.webView loadRequest:request];
} else {
[self.webView loadHTMLString:[self editHtml:_url]];
}
}
- (void)createSubViews {
[super createSubViews];
if(!self.closeHidden&&!self.navigationHidden){
[self.view addSubview:self.closeBtn];
}
[self.view addSubview:self.webView];
}
#pragma mark - CNLiveWebViewDelegate
- (void)webView:(CNLiveWebView *)webView didFinishLoadWithURL:(NSURL *)url {
NSLog(@"CNLiveWebViewController - didFinishLoad");
if(self.title.length == 0){
self.title = webView.title;
}
}
- (void)webView:(CNLiveWebView *)webView didFailLoadWithError:(NSError *)error{
}
#pragma mark - 响应方法
//回退goBack
- (void)goBack {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
else {
[self closeDidClicked];
}
}
//关闭pop
- (void)closeDidClicked {
if (self.presentingViewController != nil) {
[self dismissViewControllerAnimated:YES completion:nil];
}
else {
[self.navigationController popViewControllerAnimated:YES];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - 懒加载
- (UIButton *)closeBtn{
if(!_closeBtn){
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_closeBtn.adjustsImageWhenHighlighted = NO;
_closeBtn.frame = CGRectMake(NavigationBarHeight+margin, StatusBarHeight, NavigationBarHeight, NavigationBarHeight);
UIImage *image = [self getImageWithImageName:@"cnlive_black_close" bundleName:@"CNLiveCustomControl" targetClass:[CNLiveNavigationBar class]];
[_closeBtn setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
[_closeBtn addTarget:self action:@selector(closeDidClicked) forControlEvents:UIControlEventTouchUpInside];
[_closeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
_closeBtn.hidden = self.closeHidden;
}
return _closeBtn;
}
- (CNLiveWebView *)webView{
if(!_webView){
_webView = [CNLiveWebView webViewWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop)];
_webView.delegate = self;
}
return _webView;
}
@end