-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathSWBWebViewContainer.m
276 lines (238 loc) · 8.28 KB
/
SWBWebViewContainer.m
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// SWBWebViewContainer.m
// AquaWeb
//
// Created by clowwindy on 11-6-16.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "SWBWebViewContainer.h"
#import "SWBAppDelegate.h"
//#import "WebView.h"
//#import "UIWebDocumentView.h"
@implementation SWBWebViewContainer
@synthesize delegate;
-(void)loadDefaults {
cachedWebViews = [[NSMutableArray alloc] init];
s = [[NSString alloc] initWithFormat:@"_setDraw%@:", [@"InWebThread" copy]];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self loadDefaults];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
[self loadDefaults];
}
return self;
}
- (void)setNetworkIndicatorStatus {
BOOL loading = NO;
for (SWBWebView *webView in cachedWebViews) {
loading |= webView.loading;
}
[appNetworkActivityIndicatorManager setSourceActivityStatusIsBusy:self busy:loading];
// [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:loading];
}
- (void)moveWebViewToCacheQueueEnd:(SWBWebView *)webView {
// TODO
if ([cachedWebViews containsObject:webView]) {
[cachedWebViews removeObject:webView];
}
[cachedWebViews addObject:webView];
}
//- (void)progressEstimateChanged:(NSNotification*)theNotification {
// // You can get the progress as a float with
// // [[theNotification object] estimatedProgress], and then you
// // can set that to a UIProgressView if you'd like.
// // theProgressView is just an example of what you could do.
//
//// [theProgressView setProgress:[[theNotification object] estimatedProgress]];
//
// NSLog(@"%@",[[theNotification object] estimatedProgress]);
//
// if ((int)[[theNotification object] estimatedProgress] == 1) {
//// theProgressView.hidden = TRUE;
// // Hide the progress view. This is optional, but depending on where
// // you put it, this may be a good idea.
// // If you wanted to do this, you'd
// // have to set theProgressView to visible in your
// // webViewDidStartLoad delegate method,
// // see Apple's UIWebView documentation.
// }
//}
- (void)initAWebView:(SWBWebView *)webView {
webView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
// cause crash on double-tap
@try {
SEL ss = NSSelectorFromString(s);
if ([webView respondsToSelector:ss]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIWebView instanceMethodSignatureForSelector:ss]];
BOOL setting = YES;
[invocation setSelector:ss];
[invocation setTarget:webView];
[invocation setArgument:&setting atIndex:2];
[invocation invoke];
// [webView performSelector:ss withObject:YES];
}
}
@catch (NSException *exception) {
NSLog(@"%@", exception);
}
@finally {
}
webView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight
| UIViewAutoresizingFlexibleWidth;
webView.multipleTouchEnabled = YES;
webView.scalesPageToFit = YES;
webView.delegate = self;
// UIWebDocumentView *documentView = [webView _documentView];
// WebView *coreWebView = [documentView webView];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressEstimateChanged:) name:@"WebViewProgressEstimateChangedNotification" object:coreWebView];
// [coreWebView estimatedProgress
}
- (void)removeAWebView:(SWBWebView *)webView {
webView.delegate = nil;
// // fix memory leaks
// [webView loadHTMLString:@"" baseURL:nil];
[cachedWebViews removeObject:webView];
if (webView.superview) {
[webView removeFromSuperview];
}
}
- (SWBWebView *)getANewWebView {
NSInteger count = [cachedWebViews count];
if (count >= kMaxCachedWebViews) {
[self removeAWebView:[cachedWebViews objectAtIndex:0]];
}
SWBWebView *webView = [[SWBWebView alloc] init];
[self initAWebView:webView];
[cachedWebViews addObject:webView];
return webView;
}
- (void)switchToWebViewWithWebView:(SWBWebView *)webView {
[self moveWebViewToCacheQueueEnd:webView];
// if (currentWebView) {
// [currentWebView removeFromSuperview];
// }
if (currentWebView && currentWebView != webView) {
// 让后台的网页不改变大小,以加快旋转和显示小工具的速度
currentWebView.autoresizingMask = UIViewAutoresizingNone;
// currentWebView.hidden = YES;
@try {
((UIScrollView *)[[currentWebView subviews] objectAtIndex:0]).scrollsToTop = NO;
}
@catch (NSException *exception) {
}
@finally {
}
}
if (webView.superview) {
[self bringSubviewToFront:webView];
} else {
// webView.frame = CGRectZero;
[self addSubview:webView];
}
// webView.hidden = NO;
@try {
((UIScrollView *)[[webView subviews] objectAtIndex:0]).scrollsToTop = YES;
}
@catch (NSException *exception) {
}
@finally {
}
currentWebView = webView;
// 设置前台
webView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight
| UIViewAutoresizingFlexibleWidth;
}
- (void)newWebView:(NSInteger)tag {
SWBWebView *webView = [self getANewWebView];
webView.tag = tag;
[self switchToWebViewWithWebView:webView];
[delegate webViewContainerWebViewDidCreateNew:webView];
}
- (void)switchToWebView:(NSInteger)tag {
SWBWebView *webView = [self webViewByTag:tag];
if (currentWebView && currentWebView == webView) {
// do not compare tags, because currentWebView may be released
return;
}
[self switchToWebViewWithWebView:webView];
[delegate webViewContainerWebViewDidSwitchToWebView:webView];
}
- (void)closeWebView:(NSInteger)tag {
if (tag == currentWebView.tag) {
currentWebView = nil;
}
[self removeAWebView:[self webViewByTag:tag]];
}
- (SWBWebView *)webViewByTag:(NSInteger)tag {
for (SWBWebView *webView in cachedWebViews) {
if (webView.tag == tag) {
return webView;
}
}
SWBWebView *webView = [self getANewWebView];
webView.tag = tag;
[delegate webViewContainerWebViewNeedToReload:webView tag:tag];
return webView;
}
- (NSInteger)currentWebView {
return currentWebView.tag;
}
- (SWBWebView *)currentSWBWebView {
return currentWebView;
}
- (void)releaseBackgroundWebViews {
NSInteger count = [cachedWebViews count];
for (int i = 0; i < count - kMinCachedWebViews; i ++) {
[(UIWebView *)[cachedWebViews objectAtIndex:0] removeFromSuperview];
[cachedWebViews removeObjectAtIndex:0];
}
}
- (void)removeBackgroundWebViewsFromSuperView {
NSInteger count = [cachedWebViews count];
for (int i = 0; i < count - kMinCachedWebViews; i ++) {
[(UIWebView *)[cachedWebViews objectAtIndex:i] removeFromSuperview];
}
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[delegate webView:webView didFailLoadWithError:error];
[self setNetworkIndicatorStatus];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (delegate) {
[delegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}
return YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[delegate webViewDidFinishLoad:webView];
[self setNetworkIndicatorStatus];
}
-(void)webViewDidStartLoad:(UIWebView *)webView {
[delegate webViewDidStartLoad:webView];
[self setNetworkIndicatorStatus];
}
-(NSString *)titleForWebView:(SWBWebView *)webView {
NSString *theTitle = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
return theTitle;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end