|
@@ -9,7 +9,7 @@
|
9
|
9
|
#import <React/RCTConvert.h>
|
10
|
10
|
#import <React/RCTAutoInsetsProtocol.h>
|
11
|
11
|
#import "RNCWKProcessPoolManager.h"
|
12
|
|
-#import <UIKit/UIKit.h>
|
|
12
|
+#import <React/RCTUIKit.h>
|
13
|
13
|
|
14
|
14
|
#import "objc/runtime.h"
|
15
|
15
|
|
|
@@ -19,6 +19,7 @@ static NSString *const MessageHandlerName = @"ReactNativeWebView";
|
19
|
19
|
static NSURLCredential* clientAuthenticationCredential;
|
20
|
20
|
static NSDictionary* customCertificatesForHost;
|
21
|
21
|
|
|
22
|
+#if !TARGET_OS_OSX
|
22
|
23
|
// runtime trick to remove WKWebView keyboard default toolbar
|
23
|
24
|
// see: http://stackoverflow.com/questions/19033292/ios-7-uiwebview-keyboard-issue/19042279#19042279
|
24
|
25
|
@interface _SwizzleHelperWK : UIView
|
|
@@ -39,8 +40,13 @@ static NSDictionary* customCertificatesForHost;
|
39
|
40
|
return nil;
|
40
|
41
|
}
|
41
|
42
|
@end
|
|
43
|
+#endif // !TARGET_OS_OSX
|
42
|
44
|
|
43
|
|
-@interface RNCWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, UIScrollViewDelegate, RCTAutoInsetsProtocol>
|
|
45
|
+@interface RNCWebView () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler,
|
|
46
|
+#if !TARGET_OS_OSX
|
|
47
|
+ UIScrollViewDelegate,
|
|
48
|
+#endif // !TARGET_OS_OSX
|
|
49
|
+ RCTAutoInsetsProtocol>
|
44
|
50
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingStart;
|
45
|
51
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingFinish;
|
46
|
52
|
@property (nonatomic, copy) RCTDirectEventBlock onLoadingError;
|
|
@@ -55,14 +61,16 @@ static NSDictionary* customCertificatesForHost;
|
55
|
61
|
|
56
|
62
|
@implementation RNCWebView
|
57
|
63
|
{
|
58
|
|
- UIColor * _savedBackgroundColor;
|
|
64
|
+ RCTUIColor * _savedBackgroundColor;
|
59
|
65
|
BOOL _savedHideKeyboardAccessoryView;
|
60
|
66
|
BOOL _savedKeyboardDisplayRequiresUserAction;
|
61
|
67
|
|
62
|
68
|
// Workaround for StatusBar appearance bug for iOS 12
|
63
|
69
|
// https://github.com/react-native-community/react-native-webview/issues/62
|
64
|
70
|
BOOL _isFullScreenVideoOpen;
|
|
71
|
+#if !TARGET_OS_OSX
|
65
|
72
|
UIStatusBarStyle _savedStatusBarStyle;
|
|
73
|
+#endif // !TARGET_OS_OSX
|
66
|
74
|
BOOL _savedStatusBarHidden;
|
67
|
75
|
|
68
|
76
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
|
@@ -73,7 +81,7 @@ static NSDictionary* customCertificatesForHost;
|
73
|
81
|
- (instancetype)initWithFrame:(CGRect)frame
|
74
|
82
|
{
|
75
|
83
|
if ((self = [super initWithFrame:frame])) {
|
76
|
|
- super.backgroundColor = [UIColor clearColor];
|
|
84
|
+ super.backgroundColor = [RCTUIColor clearColor];
|
77
|
85
|
_bounces = YES;
|
78
|
86
|
_scrollEnabled = YES;
|
79
|
87
|
_showsHorizontalScrollIndicator = YES;
|
|
@@ -82,14 +90,17 @@ static NSDictionary* customCertificatesForHost;
|
82
|
90
|
_automaticallyAdjustContentInsets = YES;
|
83
|
91
|
_contentInset = UIEdgeInsetsZero;
|
84
|
92
|
_savedKeyboardDisplayRequiresUserAction = YES;
|
|
93
|
+#if !TARGET_OS_OSX
|
85
|
94
|
_savedStatusBarStyle = RCTSharedApplication().statusBarStyle;
|
86
|
95
|
_savedStatusBarHidden = RCTSharedApplication().statusBarHidden;
|
|
96
|
+#endif // !TARGET_OS_OSX
|
87
|
97
|
|
88
|
98
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
89
|
99
|
_savedContentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
90
|
100
|
#endif
|
91
|
101
|
}
|
92
|
102
|
|
|
103
|
+#if !TARGET_OS_OSX
|
93
|
104
|
if (@available(iOS 12.0, *)) {
|
94
|
105
|
// Workaround for a keyboard dismissal bug present in iOS 12
|
95
|
106
|
// https://openradar.appspot.com/radar?id=5018321736957952
|
|
@@ -114,7 +125,7 @@ static NSDictionary* customCertificatesForHost;
|
114
|
125
|
name:UIWindowDidBecomeHiddenNotification
|
115
|
126
|
object:nil];
|
116
|
127
|
}
|
117
|
|
-
|
|
128
|
+#endif // !TARGET_OS_OSX
|
118
|
129
|
return self;
|
119
|
130
|
}
|
120
|
131
|
|
|
@@ -209,6 +220,7 @@ static NSDictionary* customCertificatesForHost;
|
209
|
220
|
}
|
210
|
221
|
}
|
211
|
222
|
|
|
223
|
+#if !TARGET_OS_OSX
|
212
|
224
|
wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
|
213
|
225
|
#if WEBKIT_IOS_10_APIS_AVAILABLE
|
214
|
226
|
wkWebViewConfig.mediaTypesRequiringUserActionForPlayback = _mediaPlaybackRequiresUserAction
|
|
@@ -218,6 +230,7 @@ static NSDictionary* customCertificatesForHost;
|
218
|
230
|
#else
|
219
|
231
|
wkWebViewConfig.mediaPlaybackRequiresUserAction = _mediaPlaybackRequiresUserAction;
|
220
|
232
|
#endif
|
|
233
|
+#endif // !TARGET_OS_OSX
|
221
|
234
|
|
222
|
235
|
if (_applicationNameForUserAgent) {
|
223
|
236
|
wkWebViewConfig.applicationNameForUserAgent = [NSString stringWithFormat:@"%@ %@", wkWebViewConfig.applicationNameForUserAgent, _applicationNameForUserAgent];
|
|
@@ -287,15 +300,19 @@ static NSDictionary* customCertificatesForHost;
|
287
|
300
|
|
288
|
301
|
_webView = [[WKWebView alloc] initWithFrame:self.bounds configuration: wkWebViewConfig];
|
289
|
302
|
[self setBackgroundColor: _savedBackgroundColor];
|
|
303
|
+#if !TARGET_OS_OSX
|
290
|
304
|
_webView.scrollView.delegate = self;
|
|
305
|
+#endif // !TARGET_OS_OSX
|
291
|
306
|
_webView.UIDelegate = self;
|
292
|
307
|
_webView.navigationDelegate = self;
|
|
308
|
+#if !TARGET_OS_OSX
|
293
|
309
|
_webView.scrollView.scrollEnabled = _scrollEnabled;
|
294
|
310
|
_webView.scrollView.pagingEnabled = _pagingEnabled;
|
295
|
311
|
_webView.scrollView.bounces = _bounces;
|
296
|
312
|
_webView.scrollView.showsHorizontalScrollIndicator = _showsHorizontalScrollIndicator;
|
297
|
313
|
_webView.scrollView.showsVerticalScrollIndicator = _showsVerticalScrollIndicator;
|
298
|
314
|
_webView.scrollView.directionalLockEnabled = _directionalLockEnabled;
|
|
315
|
+#endif // !TARGET_OS_OSX
|
299
|
316
|
_webView.allowsLinkPreview = _allowsLinkPreview;
|
300
|
317
|
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
|
301
|
318
|
_webView.allowsBackForwardNavigationGestures = _allowsBackForwardNavigationGestures;
|
|
@@ -329,13 +346,16 @@ static NSDictionary* customCertificatesForHost;
|
329
|
346
|
[_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHandlerName];
|
330
|
347
|
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
|
331
|
348
|
[_webView removeFromSuperview];
|
|
349
|
+#if !TARGET_OS_OSX
|
332
|
350
|
_webView.scrollView.delegate = nil;
|
|
351
|
+#endif // !TARGET_OS_OSX
|
333
|
352
|
_webView = nil;
|
334
|
353
|
}
|
335
|
354
|
|
336
|
355
|
[super removeFromSuperview];
|
337
|
356
|
}
|
338
|
357
|
|
|
358
|
+#if !TARGET_OS_OSX
|
339
|
359
|
-(void)showFullScreenVideoStatusBars
|
340
|
360
|
{
|
341
|
361
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
@@ -383,6 +403,7 @@ static NSDictionary* customCertificatesForHost;
|
383
|
403
|
}];
|
384
|
404
|
}
|
385
|
405
|
}
|
|
406
|
+#endif // !TARGET_OS_OSX
|
386
|
407
|
|
387
|
408
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
|
388
|
409
|
if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
|
|
@@ -396,7 +417,7 @@ static NSDictionary* customCertificatesForHost;
|
396
|
417
|
}
|
397
|
418
|
}
|
398
|
419
|
|
399
|
|
-- (void)setBackgroundColor:(UIColor *)backgroundColor
|
|
420
|
+- (void)setBackgroundColor:(RCTUIColor *)backgroundColor
|
400
|
421
|
{
|
401
|
422
|
_savedBackgroundColor = backgroundColor;
|
402
|
423
|
if (_webView == nil) {
|
|
@@ -404,9 +425,11 @@ static NSDictionary* customCertificatesForHost;
|
404
|
425
|
}
|
405
|
426
|
|
406
|
427
|
CGFloat alpha = CGColorGetAlpha(backgroundColor.CGColor);
|
|
428
|
+#if !TARGET_OS_OSX
|
407
|
429
|
self.opaque = _webView.opaque = (alpha == 1.0);
|
408
|
430
|
_webView.scrollView.backgroundColor = backgroundColor;
|
409
|
431
|
_webView.backgroundColor = backgroundColor;
|
|
432
|
+#endif // !TARGET_OS_OSX
|
410
|
433
|
}
|
411
|
434
|
|
412
|
435
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
|
|
@@ -473,14 +496,22 @@ static NSDictionary* customCertificatesForHost;
|
473
|
496
|
{
|
474
|
497
|
_contentInset = contentInset;
|
475
|
498
|
[RCTView autoAdjustInsetsForView:self
|
|
499
|
+#if !TARGET_OS_OSX
|
476
|
500
|
withScrollView:_webView.scrollView
|
|
501
|
+#else
|
|
502
|
+ withScrollView:nil
|
|
503
|
+#endif // !TARGET_OS_OSX
|
477
|
504
|
updateOffset:NO];
|
478
|
505
|
}
|
479
|
506
|
|
480
|
507
|
- (void)refreshContentInset
|
481
|
508
|
{
|
482
|
509
|
[RCTView autoAdjustInsetsForView:self
|
|
510
|
+#if !TARGET_OS_OSX
|
483
|
511
|
withScrollView:_webView.scrollView
|
|
512
|
+#else
|
|
513
|
+ withScrollView:nil
|
|
514
|
+#endif // !TARGET_OS_OSX
|
484
|
515
|
updateOffset:YES];
|
485
|
516
|
}
|
486
|
517
|
|
|
@@ -519,6 +550,7 @@ static NSDictionary* customCertificatesForHost;
|
519
|
550
|
}
|
520
|
551
|
}
|
521
|
552
|
|
|
553
|
+#if !TARGET_OS_OSX
|
522
|
554
|
-(void)setKeyboardDisplayRequiresUserAction:(BOOL)keyboardDisplayRequiresUserAction
|
523
|
555
|
{
|
524
|
556
|
if (_webView == nil) {
|
|
@@ -623,19 +655,24 @@ static NSDictionary* customCertificatesForHost;
|
623
|
655
|
|
624
|
656
|
object_setClass(subview, newClass);
|
625
|
657
|
}
|
|
658
|
+#endif // !TARGET_OS_OSX
|
626
|
659
|
|
627
|
|
-- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
|
|
660
|
+- (void)scrollViewWillBeginDragging:(RCTUIScrollView *)scrollView
|
628
|
661
|
{
|
|
662
|
+#if !TARGET_OS_OSX
|
629
|
663
|
scrollView.decelerationRate = _decelerationRate;
|
|
664
|
+#endif // !TARGET_OS_OSX
|
630
|
665
|
}
|
631
|
666
|
|
632
|
667
|
- (void)setScrollEnabled:(BOOL)scrollEnabled
|
633
|
668
|
{
|
634
|
669
|
_scrollEnabled = scrollEnabled;
|
|
670
|
+#if !TARGET_OS_OSX
|
635
|
671
|
_webView.scrollView.scrollEnabled = scrollEnabled;
|
|
672
|
+#endif // !TARGET_OS_OSX
|
636
|
673
|
}
|
637
|
674
|
|
638
|
|
-- (void)scrollViewDidScroll:(UIScrollView *)scrollView
|
|
675
|
+- (void)scrollViewDidScroll:(RCTUIScrollView *)scrollView
|
639
|
676
|
{
|
640
|
677
|
// Don't allow scrolling the scrollView.
|
641
|
678
|
if (!_scrollEnabled) {
|
|
@@ -670,19 +707,25 @@ static NSDictionary* customCertificatesForHost;
|
670
|
707
|
- (void)setDirectionalLockEnabled:(BOOL)directionalLockEnabled
|
671
|
708
|
{
|
672
|
709
|
_directionalLockEnabled = directionalLockEnabled;
|
|
710
|
+#if !TARGET_OS_OSX
|
673
|
711
|
_webView.scrollView.directionalLockEnabled = directionalLockEnabled;
|
|
712
|
+#endif // !TARGET_OS_OSX
|
674
|
713
|
}
|
675
|
714
|
|
676
|
715
|
- (void)setShowsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator
|
677
|
716
|
{
|
678
|
717
|
_showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
|
|
718
|
+#if !TARGET_OS_OSX
|
679
|
719
|
_webView.scrollView.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
|
|
720
|
+#endif // !TARGET_OS_OSX
|
680
|
721
|
}
|
681
|
722
|
|
682
|
723
|
- (void)setShowsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator
|
683
|
724
|
{
|
684
|
725
|
_showsVerticalScrollIndicator = showsVerticalScrollIndicator;
|
|
726
|
+#if !TARGET_OS_OSX
|
685
|
727
|
_webView.scrollView.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
|
|
728
|
+#endif // !TARGET_OS_OSX
|
686
|
729
|
}
|
687
|
730
|
|
688
|
731
|
- (void)postMessage:(NSString *)message
|
|
@@ -701,7 +744,9 @@ static NSDictionary* customCertificatesForHost;
|
701
|
744
|
|
702
|
745
|
// Ensure webview takes the position and dimensions of RNCWebView
|
703
|
746
|
_webView.frame = self.bounds;
|
|
747
|
+#if !TARGET_OS_OSX
|
704
|
748
|
_webView.scrollView.contentInset = _contentInset;
|
|
749
|
+#endif // !TARGET_OS_OSX
|
705
|
750
|
}
|
706
|
751
|
|
707
|
752
|
- (NSMutableDictionary<NSString *, id> *)baseEvent
|
|
@@ -767,18 +812,22 @@ static NSDictionary* customCertificatesForHost;
|
767
|
812
|
*/
|
768
|
813
|
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
|
769
|
814
|
{
|
|
815
|
+#if !TARGET_OS_OSX
|
770
|
816
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
|
771
|
817
|
[alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
|
772
|
818
|
completionHandler();
|
773
|
819
|
}]];
|
774
|
820
|
[[self topViewController] presentViewController:alert animated:YES completion:NULL];
|
775
|
|
-
|
|
821
|
+#else
|
|
822
|
+ // TODO
|
|
823
|
+#endif // !TARGET_OS_OSX
|
776
|
824
|
}
|
777
|
825
|
|
778
|
826
|
/**
|
779
|
827
|
* confirm
|
780
|
828
|
*/
|
781
|
829
|
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
|
|
830
|
+#if !TARGET_OS_OSX
|
782
|
831
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
|
783
|
832
|
[alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
|
784
|
833
|
completionHandler(YES);
|
|
@@ -787,12 +836,16 @@ static NSDictionary* customCertificatesForHost;
|
787
|
836
|
completionHandler(NO);
|
788
|
837
|
}]];
|
789
|
838
|
[[self topViewController] presentViewController:alert animated:YES completion:NULL];
|
|
839
|
+#else
|
|
840
|
+ // TODO
|
|
841
|
+#endif // !TARGET_OS_OSX
|
790
|
842
|
}
|
791
|
843
|
|
792
|
844
|
/**
|
793
|
845
|
* prompt
|
794
|
846
|
*/
|
795
|
847
|
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString *))completionHandler{
|
|
848
|
+#if !TARGET_OS_OSX
|
796
|
849
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:prompt preferredStyle:UIAlertControllerStyleAlert];
|
797
|
850
|
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
798
|
851
|
textField.text = defaultText;
|
|
@@ -807,8 +860,12 @@ static NSDictionary* customCertificatesForHost;
|
807
|
860
|
[alert addAction:cancelAction];
|
808
|
861
|
alert.preferredAction = okAction;
|
809
|
862
|
[[self topViewController] presentViewController:alert animated:YES completion:NULL];
|
|
863
|
+#else
|
|
864
|
+ // TODO
|
|
865
|
+#endif // !TARGET_OS_OSX
|
810
|
866
|
}
|
811
|
867
|
|
|
868
|
+#if !TARGET_OS_OSX
|
812
|
869
|
/**
|
813
|
870
|
* topViewController
|
814
|
871
|
*/
|
|
@@ -847,7 +904,7 @@ static NSDictionary* customCertificatesForHost;
|
847
|
904
|
}
|
848
|
905
|
return window;
|
849
|
906
|
}
|
850
|
|
-
|
|
907
|
+#endif // !TARGET_OS_OSX
|
851
|
908
|
|
852
|
909
|
/**
|
853
|
910
|
* Decides whether to allow or cancel a navigation.
|
|
@@ -1055,7 +1112,9 @@ static NSDictionary* customCertificatesForHost;
|
1055
|
1112
|
- (void)setBounces:(BOOL)bounces
|
1056
|
1113
|
{
|
1057
|
1114
|
_bounces = bounces;
|
|
1115
|
+#if !TARGET_OS_OSX
|
1058
|
1116
|
_webView.scrollView.bounces = bounces;
|
|
1117
|
+#endif // !TARGET_OS_OSX
|
1059
|
1118
|
}
|
1060
|
1119
|
|
1061
|
1120
|
- (NSURLRequest *)requestForSource:(id)json {
|