This repository was archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathDZNWebViewController.m
More file actions
924 lines (734 loc) · 29 KB
/
DZNWebViewController.m
File metadata and controls
924 lines (734 loc) · 29 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
//
// DZNWebViewController.m
// DZNWebViewController
// https://github.com/dzenbot/DZNWebViewController
//
// Created by Ignacio Romero Zurbuchen on 10/25/13.
// Copyright (c) 2014 DZN Labs. All rights reserved.
// Licence: MIT-Licence
//
#import "DZNWebViewController.h"
#import "DZNPolyActivity.h"
#define DZN_IS_IPAD [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad
#define DZN_IS_LANDSCAPE ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
static char DZNWebViewControllerKVOContext = 0;
@interface DZNWebViewController ()
@property (nonatomic, strong) UIBarButtonItem *backwardBarItem;
@property (nonatomic, strong) UIBarButtonItem *forwardBarItem;
@property (nonatomic, strong) UIBarButtonItem *stateBarItem;
@property (nonatomic, strong) UIBarButtonItem *actionBarItem;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, strong) UILongPressGestureRecognizer *backwardLongPress;
@property (nonatomic, strong) UILongPressGestureRecognizer *forwardLongPress;
@property (nonatomic, weak) UIToolbar *toolbar;
@property (nonatomic, weak) UINavigationBar *navigationBar;
@property (nonatomic, weak) UIView *navigationBarSuperView;
@property (nonatomic) BOOL completedInitialLoad;
@end
@implementation DZNWebViewController
@synthesize URL = _URL;
@synthesize fileURL = _fileURL;
- (instancetype)init
{
self = [super init];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithURL:(NSURL *)URL
{
NSParameterAssert(URL);
self = [self init];
if (self) {
_URL = URL;
_fileURL = nil;
}
return self;
}
- (instancetype)initWithFileURL:(NSURL *)URL
{
self = [self init];
if (self) {
_URL = nil;
_fileURL = URL;
}
return self;
}
- (void)awakeFromNib
{
[self commonInit];
}
- (void)commonInit
{
self.supportedWebNavigationTools = DZNWebNavigationToolAll;
self.supportedWebActions = DZNWebActionAll;
self.webNavigationPrompt = DZNWebNavigationPromptAll;
self.showLoadingProgress = YES;
self.hideBarsWithGestures = YES;
self.allowHistory = YES;
self.webView = [[DZNWebView alloc] initWithFrame:self.view.bounds configuration:[WKWebViewConfiguration new]];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.allowsBackForwardNavigationGestures = YES;
self.webView.UIDelegate = self;
self.webView.navDelegate = self;
self.webView.scrollView.delegate = self;
[self.webView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:&DZNWebViewControllerKVOContext];
self.completedInitialLoad = NO;
}
#pragma mark - View lifecycle
- (void)loadView
{
[super loadView];
self.view = self.webView;
self.automaticallyAdjustsScrollViewInsets = YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.completedInitialLoad) {
[UIView performWithoutAnimation:^{
[self configureToolBars];
}];
self.completedInitialLoad = YES;
}
if (!self.webView.URL) {
if (self.URL != nil) {
[self loadURL:self.URL];
} else if (self.fileURL != nil) {
[self loadFileURL:self.fileURL];
}
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
static dispatch_once_t didAppearConfig;
dispatch_once(&didAppearConfig, ^{
[self configureBarItemsGestures];
});
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self clearProgressViewAnimated:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.webView stopLoading];
}
#pragma mark - Getter methods
- (UIProgressView *)progressView
{
if (!_progressView)
{
CGFloat lineHeight = 2.0f;
CGRect frame = CGRectMake(0, CGRectGetHeight(self.navigationBar.bounds) - lineHeight, CGRectGetWidth(self.navigationBar.bounds), lineHeight);
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:frame];
progressView.trackTintColor = [UIColor clearColor];
progressView.alpha = 0.0f;
[self.navigationBar addSubview:progressView];
_progressView = progressView;
}
return _progressView;
}
- (UIBarButtonItem *)backwardBarItem
{
if (!_backwardBarItem)
{
_backwardBarItem = [[UIBarButtonItem alloc] initWithImage:[self backwardButtonImage] landscapeImagePhone:nil style:0 target:self action:@selector(goBackward:)];
_backwardBarItem.accessibilityLabel = NSLocalizedStringFromTable(@"Backward", @"DZNWebViewController", @"Accessibility label button title");
_backwardBarItem.enabled = NO;
}
return _backwardBarItem;
}
- (UIBarButtonItem *)forwardBarItem
{
if (!_forwardBarItem)
{
_forwardBarItem = [[UIBarButtonItem alloc] initWithImage:[self forwardButtonImage] landscapeImagePhone:nil style:0 target:self action:@selector(goForward:)];
_forwardBarItem.landscapeImagePhone = nil;
_forwardBarItem.accessibilityLabel = NSLocalizedStringFromTable(@"Forward", @"DZNWebViewController", @"Accessibility label button title");
_forwardBarItem.enabled = NO;
}
return _forwardBarItem;
}
- (UIBarButtonItem *)stateBarItem
{
if (!_stateBarItem)
{
_stateBarItem = [[UIBarButtonItem alloc] initWithImage:nil landscapeImagePhone:nil style:0 target:nil action:nil];
[self updateStateBarItem];
}
return _stateBarItem;
}
- (UIBarButtonItem *)actionBarItem
{
if (!_actionBarItem)
{
_actionBarItem = [[UIBarButtonItem alloc] initWithImage:[self actionButtonImage] landscapeImagePhone:nil style:0 target:self action:@selector(presentActivityController:)];
_actionBarItem.accessibilityLabel = NSLocalizedStringFromTable(@"Share", @"DZNWebViewController", @"Accessibility label button title");
_actionBarItem.enabled = NO;
}
return _actionBarItem;
}
- (NSArray *)navigationToolItems
{
NSMutableArray *items = [NSMutableArray new];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
if ((self.supportedWebNavigationTools & DZNWebNavigationToolBackward) > 0 || self.supportsAllNavigationTools) {
[items addObject:self.backwardBarItem];
}
if ((self.supportedWebNavigationTools & DZNWebNavigationToolForward) > 0 || self.supportsAllNavigationTools) {
if (!DZN_IS_IPAD) [items addObject:flexibleSpace];
[items addObject:self.forwardBarItem];
}
if ((self.supportedWebNavigationTools & DZNWebNavigationToolStopReload) > 0 || self.supportsAllNavigationTools) {
if (!DZN_IS_IPAD) [items addObject:flexibleSpace];
[items addObject:self.stateBarItem];
}
if (self.supportedWebActions > 0) {
if (!DZN_IS_IPAD) [items addObject:flexibleSpace];
[items addObject:self.actionBarItem];
}
return items;
}
- (BOOL)supportsAllNavigationTools
{
return (_supportedWebNavigationTools == DZNWebNavigationToolAll) ? YES : NO;
}
- (UIImage *)backwardButtonImage
{
if (!_backwardButtonImage) {
_backwardButtonImage = [UIImage imageNamed:@"dzn_icn_toolbar_backward" inBundle:[NSBundle bundleForClass:[DZNWebViewController class]] compatibleWithTraitCollection:nil];
}
return _backwardButtonImage;
}
- (UIImage *)forwardButtonImage
{
if (!_forwardButtonImage) {
_forwardButtonImage = [UIImage imageNamed:@"dzn_icn_toolbar_forward" inBundle:[NSBundle bundleForClass:[DZNWebViewController class]] compatibleWithTraitCollection:nil];
}
return _forwardButtonImage;
}
- (UIImage *)reloadButtonImage
{
if (!_reloadButtonImage) {
_reloadButtonImage = [UIImage imageNamed:@"dzn_icn_toolbar_reload" inBundle:[NSBundle bundleForClass:[DZNWebViewController class]] compatibleWithTraitCollection:nil];
}
return _reloadButtonImage;
}
- (UIImage *)stopButtonImage
{
if (!_stopButtonImage) {
_stopButtonImage = [UIImage imageNamed:@"dzn_icn_toolbar_stop" inBundle:[NSBundle bundleForClass:[DZNWebViewController class]] compatibleWithTraitCollection:nil];
}
return _stopButtonImage;
}
- (UIImage *)actionButtonImage
{
if (!_actionButtonImage) {
_actionButtonImage = [UIImage imageNamed:@"dzn_icn_toolbar_action" inBundle:[NSBundle bundleForClass:[DZNWebViewController class]] compatibleWithTraitCollection:nil];
}
return _actionButtonImage;
}
- (NSArray *)applicationActivitiesForItem:(id)item
{
NSMutableArray *activities = [NSMutableArray new];
if ([item isKindOfClass:[UIImage class]]) {
return activities;
}
if ((_supportedWebActions & DZNWebActionCopyLink) > 0 || self.supportsAllActions) {
[activities addObject:[DZNPolyActivity activityWithType:DZNPolyActivityTypeLink]];
}
if ((_supportedWebActions & DZNWebActionOpenSafari) > 0 || self.supportsAllActions) {
[activities addObject:[DZNPolyActivity activityWithType:DZNPolyActivityTypeSafari]];
}
if ((_supportedWebActions & DZNWebActionOpenChrome) > 0 || self.supportsAllActions) {
[activities addObject:[DZNPolyActivity activityWithType:DZNPolyActivityTypeChrome]];
}
if ((_supportedWebActions & DZNWebActionOpenOperaMini) > 0 || self.supportsAllActions) {
[activities addObject:[DZNPolyActivity activityWithType:DZNPolyActivityTypeOpera]];
}
if ((_supportedWebActions & DZNWebActionOpenDolphin) > 0 || self.supportsAllActions) {
[activities addObject:[DZNPolyActivity activityWithType:DZNPolyActivityTypeDolphin]];
}
return activities;
}
- (NSArray *)excludedActivityTypesForItem:(id)item
{
NSMutableArray *types = [NSMutableArray new];
if (![item isKindOfClass:[UIImage class]]) {
[types addObjectsFromArray:@[UIActivityTypeCopyToPasteboard,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePostToFlickr,
UIActivityTypePrint,
UIActivityTypeAssignToContact]];
}
if (self.supportsAllActions) {
return types;
}
if ((_supportedWebActions & DZNsupportedWebActionshareLink) == 0) {
[types addObjectsFromArray:@[UIActivityTypeMail, UIActivityTypeMessage,
UIActivityTypePostToFacebook, UIActivityTypePostToTwitter,
UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop]];
}
if ((_supportedWebActions & DZNWebActionReadLater) == 0 && [item isKindOfClass:[UIImage class]]) {
[types addObject:UIActivityTypeAddToReadingList];
}
return types;
}
- (BOOL)supportsAllActions
{
return (_supportedWebActions == DZNWebActionAll) ? YES : NO;
}
#pragma mark - Setter methods
- (void)setURL:(NSURL *)URL
{
if ([self.URL isEqual:URL]) {
return;
}
if (self.isViewLoaded) {
if ([URL isFileURL]) {
[self loadFileURL:URL];
_fileURL = URL;
_URL = nil;
} else {
[self loadURL:URL];
_URL = URL;
_fileURL = nil;
}
}
}
- (void)setTitle:(NSString *)title
{
if (self.webNavigationPrompt == DZNWebNavigationPromptNone) {
[super setTitle:title];
return;
}
NSString *url = self.webView.URL.absoluteString;
UILabel *label = (UILabel *)self.navigationItem.titleView;
if (!label || ![label isKindOfClass:[UILabel class]]) {
label = [UILabel new];
label.numberOfLines = 2;
label.textAlignment = NSTextAlignmentCenter;
label.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = label;
}
UIFont *titleFont = self.navigationBar.titleTextAttributes[NSFontAttributeName] ? : [UIFont boldSystemFontOfSize:14.0];
UIFont *urlFont = [UIFont fontWithName:titleFont.fontName size:titleFont.pointSize-2.0];
UIColor *textColor = self.navigationBar.titleTextAttributes[NSForegroundColorAttributeName] ? : [UIColor blackColor];
NSMutableString *text = [NSMutableString new];
if (title.length > 0 && self.showNavigationPromptTitle) {
[text appendFormat:@"%@", title];
if (url.length > 0 && self.showNavigationPromptURL) {
[text appendFormat:@"\n"];
}
}
if (url.length > 0 && self.showNavigationPromptURL) {
[text appendFormat:@"%@", url];
}
NSDictionary *attributes = @{NSFontAttributeName: titleFont, NSForegroundColorAttributeName: textColor};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
NSRange urlRange = [text rangeOfString:url];
if (urlRange.location != NSNotFound && self.showNavigationPromptTitle) {
[attributedString addAttribute:NSFontAttributeName value:urlFont range:urlRange];
}
label.attributedText = attributedString;
[label sizeToFit];
CGRect frame = label.frame;
frame.size.height = CGRectGetHeight(self.navigationController.navigationBar.frame);
label.frame = frame;
}
// Sets the request errors with an alert view.
- (void)setLoadingError:(NSError *)error
{
switch (error.code) {
case NSURLErrorUnknown:
case NSURLErrorCancelled: return;
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:error.localizedDescription delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles: nil];
[alert show];
}
- (BOOL)showNavigationPromptTitle
{
if ((self.webNavigationPrompt & DZNWebNavigationPromptTitle) > 0 || self.webNavigationPrompt == DZNWebNavigationPromptAll) {
return YES;
}
return NO;
}
- (BOOL)showNavigationPromptURL
{
if ((self.webNavigationPrompt & DZNWebNavigationPromptURL) > 0 || self.webNavigationPrompt == DZNWebNavigationPromptAll) {
return YES;
}
return NO;
}
#pragma mark - DZNWebViewController methods
- (void)loadURL:(NSURL *)URL
{
NSURL *baseURL = [[NSURL alloc] initFileURLWithPath:URL.path.stringByDeletingLastPathComponent isDirectory:YES];
[self loadURL:URL baseURL:baseURL];
}
- (void)loadFileURL:(NSURL *)URL
{
[self.webView loadFileURL:URL allowingReadAccessToURL:URL];
}
- (void)loadURL:(NSURL *)URL baseURL:(NSURL *)baseURL
{
if ([URL isFileURL]) {
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
NSString *HTMLString = [[NSString alloc] initWithData:data encoding:NSStringEncodingConversionAllowLossy];
[self.webView loadHTMLString:HTMLString baseURL:baseURL];
}
else {
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL];
[self.webView loadRequest:request];
}
}
- (void)goBackward:(id)sender
{
if ([self.webView canGoBack]) {
[self.webView goBack];
}
}
- (void)goForward:(id)sender
{
if ([self.webView canGoForward]) {
[self.webView goForward];
}
}
- (void)dismissHistoryController
{
if (self.presentedViewController) {
[self.presentedViewController dismissViewControllerAnimated:YES completion:^{
// The bar button item's gestures are invalidated after using them, so we must re-assign them.
[self configureBarItemsGestures];
}];
}
}
- (void)showBackwardHistory:(UIGestureRecognizer *)sender
{
if (!self.allowHistory || self.webView.backForwardList.backList.count == 0 || sender.state != UIGestureRecognizerStateBegan) {
return;
}
[self presentHistoryControllerForTool:DZNWebNavigationToolBackward fromView:sender.view];
}
- (void)showForwardHistory:(UIGestureRecognizer *)sender
{
if (!self.allowHistory || self.webView.backForwardList.forwardList.count == 0 || sender.state != UIGestureRecognizerStateBegan) {
return;
}
[self presentHistoryControllerForTool:DZNWebNavigationToolForward fromView:sender.view];
}
- (void)presentHistoryControllerForTool:(DZNWebNavigationTools)tool fromView:(UIView *)view
{
UITableViewController *controller = [UITableViewController new];
controller.title = NSLocalizedStringFromTable(@"History", @"DZNWebViewController", nil);
controller.tableView.delegate = self;
controller.tableView.dataSource = self;
controller.tableView.tag = tool;
controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissHistoryController)];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
UIView *bar = DZN_IS_IPAD ? self.navigationBar : self.toolbar;
if (DZN_IS_IPAD) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
[popover presentPopoverFromRect:view.frame inView:bar permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else {
[self presentViewController:navigationController animated:YES completion:NULL];
}
}
- (void)configureToolBars
{
if (DZN_IS_IPAD) {
self.navigationItem.rightBarButtonItems = [[[self navigationToolItems] reverseObjectEnumerator] allObjects];
}
else {
[self setToolbarItems:[self navigationToolItems]];
}
self.toolbar = self.navigationController.toolbar;
self.navigationBar = self.navigationController.navigationBar;
self.navigationBarSuperView = self.navigationBar.superview;
self.navigationController.hidesBarsOnSwipe = self.hideBarsWithGestures;
self.navigationController.hidesBarsWhenKeyboardAppears = self.hideBarsWithGestures;
self.navigationController.hidesBarsWhenVerticallyCompact = self.hideBarsWithGestures;
if (self.hideBarsWithGestures) {
[self.navigationBar addObserver:self forKeyPath:@"hidden" options:NSKeyValueObservingOptionNew context:&DZNWebViewControllerKVOContext];
[self.navigationBar addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:&DZNWebViewControllerKVOContext];
[self.navigationBar addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:&DZNWebViewControllerKVOContext];
}
if (!DZN_IS_IPAD && self.navigationController.toolbarHidden && self.toolbarItems.count > 0) {
[self.navigationController setToolbarHidden:NO];
}
}
// Light hack for adding custom gesture recognizers to UIBarButtonItems
- (void)configureBarItemsGestures
{
UIView *backwardButton= [self.backwardBarItem valueForKey:@"view"];
if (backwardButton.gestureRecognizers.count == 0) {
if (!_backwardLongPress) {
_backwardLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showBackwardHistory:)];
}
[backwardButton addGestureRecognizer:self.backwardLongPress];
}
UIView *forwardBarButton= [self.forwardBarItem valueForKey:@"view"];
if (forwardBarButton.gestureRecognizers.count == 0) {
if (!_forwardLongPress) {
_forwardLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(showForwardHistory:)];
}
[forwardBarButton addGestureRecognizer:self.forwardLongPress];
}
}
- (void)updateToolbarItems
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self.webView isLoading]];
self.backwardBarItem.enabled = [self.webView canGoBack];
self.forwardBarItem.enabled = [self.webView canGoForward];
self.actionBarItem.enabled = !self.webView.isLoading;
[self updateStateBarItem];
}
- (void)updateStateBarItem
{
self.stateBarItem.target = self.webView;
self.stateBarItem.action = self.webView.isLoading ? @selector(stopLoading) : @selector(reload);
self.stateBarItem.image = self.webView.isLoading ? self.stopButtonImage : self.reloadButtonImage;
self.stateBarItem.landscapeImagePhone = nil;
self.stateBarItem.accessibilityLabel = NSLocalizedStringFromTable(self.webView.isLoading ? @"Stop" : @"Reload", @"DZNWebViewController", @"Accessibility label button title");
self.stateBarItem.enabled = YES;
}
- (void)presentActivityController:(id)sender
{
if (!self.webView.URL.absoluteString) {
return;
}
[self presentActivityControllerWithItem:self.webView.URL.absoluteString andTitle:self.webView.title sender:sender];
}
- (void)presentActivityControllerWithItem:(id)item andTitle:(NSString *)title sender:(id)sender
{
if (!item) {
return;
}
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[title, item] applicationActivities:[self applicationActivitiesForItem:item]];
controller.excludedActivityTypes = [self excludedActivityTypesForItem:item];
if (title) {
[controller setValue:title forKey:@"subject"];
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
controller.popoverPresentationController.barButtonItem = sender;
}
[self presentViewController:controller animated:YES completion:NULL];
}
- (void)clearProgressViewAnimated:(BOOL)animated
{
if (!_progressView) {
return;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0
animations:^{
self.progressView.alpha = 0;
} completion:^(BOOL finished) {
[self destroyProgressViewIfNeeded];
}];
}
- (void)destroyProgressViewIfNeeded
{
if (_progressView) {
[_progressView removeFromSuperview];
_progressView = nil;
}
}
#pragma mark - DZNNavigationDelegate methods
- (void)webView:(DZNWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
[self updateStateBarItem];
}
- (void)webView:(DZNWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self.webView isLoading]];
}
- (void)webView:(DZNWebView *)webView didUpdateProgress:(CGFloat)progress
{
if (!self.showLoadingProgress) {
[self destroyProgressViewIfNeeded];
return;
}
if (self.progressView.alpha == 0 && progress > 0) {
self.progressView.progress = 0;
[UIView animateWithDuration:0.2 animations:^{
self.progressView.alpha = 1.0;
}];
}
else if (self.progressView.alpha == 1.0 && progress == 1.0)
{
[UIView animateWithDuration:0.2 animations:^{
self.progressView.alpha = 0.0;
} completion:^(BOOL finished) {
self.progressView.progress = 0;
}];
}
[self.progressView setProgress:progress animated:YES];
}
- (void)webView:(DZNWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
if (self.webNavigationPrompt > DZNWebNavigationPromptNone) {
self.title = self.webView.title;
}
}
- (void)webView:(DZNWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
[self setLoadingError:error];
// if this is a cancelled error, then don't affect the title
switch (error.code) {
case NSURLErrorCancelled: return;
}
self.title = nil;
}
#pragma mark - WKUIDelegate methods
- (DZNWebView *)webView:(DZNWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
#pragma mark - UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == DZNWebNavigationToolBackward) {
return self.webView.backForwardList.backList.count;
}
if (tableView.tag == DZNWebNavigationToolForward) {
return self.webView.backForwardList.forwardList.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
WKBackForwardListItem *item = nil;
if (tableView.tag == DZNWebNavigationToolBackward) {
item = [self.webView.backForwardList.backList objectAtIndex:indexPath.row];
}
if (tableView.tag == DZNWebNavigationToolForward) {
item = [self.webView.backForwardList.forwardList objectAtIndex:indexPath.row];
}
cell.textLabel.text = item.title;
cell.detailTextLabel.text = [item.URL absoluteString];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0;
}
#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WKBackForwardListItem *item = nil;
if (tableView.tag == DZNWebNavigationToolBackward) {
item = [self.webView.backForwardList.backList objectAtIndex:indexPath.row];
}
if (tableView.tag == DZNWebNavigationToolForward) {
item = [self.webView.backForwardList.forwardList objectAtIndex:indexPath.row];
}
[self.webView goToBackForwardListItem:item];
[self dismissHistoryController];
}
#pragma mark - Key Value Observer
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context != &DZNWebViewControllerKVOContext) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if ([object isEqual:self.navigationBar]) {
// Skips for landscape orientation, since there is no status bar visible on iPhone landscape
if (DZN_IS_LANDSCAPE) {
return;
}
id new = change[NSKeyValueChangeNewKey];
if ([keyPath isEqualToString:@"hidden"] && [new boolValue] && self.navigationBar.center.y >= -2.0) {
self.navigationBar.hidden = NO;
if (!self.navigationBar.superview) {
[self.navigationBarSuperView addSubview:self.navigationBar];
}
}
if ([keyPath isEqualToString:@"center"]) {
CGPoint center = [new CGPointValue];
if (center.y < -2.0) {
center.y = -2.0;
self.navigationBar.center = center;
[UIView beginAnimations:@"DZNNavigationBarAnimation" context:nil];
for (UIView *subview in self.navigationBar.subviews) {
if (subview != self.navigationBar.subviews[0]) {
subview.alpha = 0.0;
}
}
[UIView commitAnimations];
}
}
}
if ([object isEqual:self.webView] && [keyPath isEqualToString:@"loading"]) {
[self updateToolbarItems];
}
}
#pragma mark - View Auto-Rotation
#ifdef __IPHONE_9_0
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
#else
- (NSUInteger)supportedInterfaceOrientations
#endif
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
return NO;
}
#pragma mark - View lifeterm
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
if (self.hideBarsWithGestures) {
[self.navigationBar removeObserver:self forKeyPath:@"hidden" context:&DZNWebViewControllerKVOContext];
[self.navigationBar removeObserver:self forKeyPath:@"center" context:&DZNWebViewControllerKVOContext];
[self.navigationBar removeObserver:self forKeyPath:@"alpha" context:&DZNWebViewControllerKVOContext];
}
[self.webView removeObserver:self forKeyPath:@"loading" context:&DZNWebViewControllerKVOContext];
_backwardBarItem = nil;
_forwardBarItem = nil;
_stateBarItem = nil;
_actionBarItem = nil;
_progressView = nil;
_backwardLongPress = nil;
_forwardLongPress = nil;
_webView.scrollView.delegate = nil;
_webView.navDelegate = nil;
_webView.UIDelegate = nil;
_webView.scrollView.delegate = nil;
_webView = nil;
_URL = nil;
}
@end