@@ -18,22 +18,32 @@ Licensed to the Apache Software Foundation (ASF) under one
18
18
*/
19
19
20
20
#import " CDVWebViewUIDelegate.h"
21
+ #import < Cordova/CDVViewController.h>
22
+
23
+ @interface CDVWebViewUIDelegate ()
24
+
25
+ @property (nonatomic , weak ) CDVViewController *viewController;
26
+
27
+ @end
21
28
22
29
@implementation CDVWebViewUIDelegate
30
+ {
31
+ NSMutableArray <UIViewController *> *windows;
32
+ }
23
33
24
- - (instancetype )initWithTitle : ( NSString *) title
34
+ - (instancetype )initWithViewController : (CDVViewController *) vc
25
35
{
26
36
self = [super init ];
37
+
27
38
if (self) {
28
- self.title = title;
39
+ self.viewController = vc;
40
+ self.title = vc.title ;
29
41
windows = [[NSMutableArray alloc ] init ];
30
42
}
31
-
32
43
return self;
33
44
}
34
45
35
- - (void ) webView : (WKWebView *)webView runJavaScriptAlertPanelWithMessage : (NSString *)message
36
- initiatedByFrame : (WKFrameInfo *)frame completionHandler : (void (^)(void ))completionHandler
46
+ - (void )webView : (WKWebView *)webView runJavaScriptAlertPanelWithMessage : (NSString *)message initiatedByFrame : (WKFrameInfo *)frame completionHandler : (CDV_SWIFT_UI_ACTOR void (^)(void ))completionHandler
37
47
{
38
48
UIAlertController* alert = [UIAlertController alertControllerWithTitle: self .title
39
49
message: message
@@ -49,13 +59,10 @@ - (void) webView:(WKWebView*)webView runJavaScriptAlertPanelWithMessage:(NSS
49
59
50
60
[alert addAction: ok];
51
61
52
- UIViewController* rootController = [UIApplication sharedApplication ].delegate .window .rootViewController ;
53
-
54
- [rootController presentViewController: alert animated: YES completion: nil ];
62
+ [[self topViewController ] presentViewController: alert animated: YES completion: nil ];
55
63
}
56
64
57
- - (void ) webView : (WKWebView *)webView runJavaScriptConfirmPanelWithMessage : (NSString *)message
58
- initiatedByFrame : (WKFrameInfo *)frame completionHandler : (void (^)(BOOL result))completionHandler
65
+ - (void )webView : (WKWebView *)webView runJavaScriptConfirmPanelWithMessage : (NSString *)message initiatedByFrame : (WKFrameInfo *)frame completionHandler : (CDV_SWIFT_UI_ACTOR void (^)(BOOL result))completionHandler
59
66
{
60
67
UIAlertController* alert = [UIAlertController alertControllerWithTitle: self .title
61
68
message: message
@@ -80,14 +87,10 @@ - (void) webView:(WKWebView*)webView runJavaScriptConfirmPanelWithMessage:(N
80
87
}];
81
88
[alert addAction: cancel];
82
89
83
- UIViewController* rootController = [UIApplication sharedApplication ].delegate .window .rootViewController ;
84
-
85
- [rootController presentViewController: alert animated: YES completion: nil ];
90
+ [[self topViewController ] presentViewController: alert animated: YES completion: nil ];
86
91
}
87
92
88
- - (void ) webView : (WKWebView *)webView runJavaScriptTextInputPanelWithPrompt : (NSString *)prompt
89
- defaultText : (NSString *)defaultText initiatedByFrame : (WKFrameInfo *)frame
90
- completionHandler : (void (^)(NSString * result))completionHandler
93
+ - (void )webView : (WKWebView *)webView runJavaScriptTextInputPanelWithPrompt : (NSString *)prompt defaultText : (NSString *)defaultText initiatedByFrame : (WKFrameInfo *)frame completionHandler : (CDV_SWIFT_UI_ACTOR void (^)(NSString * result))completionHandler
91
94
{
92
95
UIAlertController* alert = [UIAlertController alertControllerWithTitle: self .title
93
96
message: prompt
@@ -116,12 +119,10 @@ - (void) webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:
116
119
textField.text = defaultText;
117
120
}];
118
121
119
- UIViewController* rootController = [UIApplication sharedApplication ].delegate .window .rootViewController ;
120
-
121
- [rootController presentViewController: alert animated: YES completion: nil ];
122
+ [[self topViewController ] presentViewController: alert animated: YES completion: nil ];
122
123
}
123
124
124
- - (WKWebView *) webView : (WKWebView *)webView createWebViewWithConfiguration : (WKWebViewConfiguration *)configuration forNavigationAction : (WKNavigationAction *)navigationAction windowFeatures : (WKWindowFeatures *)windowFeatures
125
+ - (nullable WKWebView *)webView : (WKWebView *)webView createWebViewWithConfiguration : (WKWebViewConfiguration *)configuration forNavigationAction : (WKNavigationAction *)navigationAction windowFeatures : (WKWindowFeatures *)windowFeatures
125
126
{
126
127
if (!navigationAction.targetFrame .isMainFrame ) {
127
128
if (self.allowNewWindows ) {
@@ -135,8 +136,7 @@ - (WKWebView*) webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWeb
135
136
136
137
[windows addObject: vc];
137
138
138
- UIViewController* rootController = [UIApplication sharedApplication ].delegate .window .rootViewController ;
139
- [rootController presentViewController: vc animated: YES completion: nil ];
139
+ [[self topViewController ] presentViewController: vc animated: YES completion: nil ];
140
140
return v;
141
141
} else {
142
142
[webView loadRequest: navigationAction.request];
@@ -159,5 +159,17 @@ - (void)webViewDidClose:(WKWebView*)webView
159
159
// We do not allow closing the primary WebView
160
160
}
161
161
162
+ #pragma mark - Utility Methods
163
+
164
+ - (nullable UIViewController *)topViewController
165
+ {
166
+ UIViewController *vc = self.viewController ;
167
+
168
+ while (vc.presentedViewController != nil && ![vc.presentedViewController isBeingDismissed ]) {
169
+ vc = vc.presentedViewController ;
170
+ }
171
+
172
+ return vc;
173
+ }
162
174
163
175
@end
0 commit comments