Skip to content

Commit 32b21e1

Browse files
authored
Don't silently ignore creation of new windows (#885)
Currently, attempts to create new windows (via links with `target="_blank"` or JS `window.open`) for pages within the application have no effect and are silently ignored. (Links to pages outside the application trigger the allow-intent functionality and open in Safari.) With this PR, the default behaviour will be to open those links in the **same** Web View (without creating a new window). This also introduces a preference (`AllowNewWindows`) that allows the creation of a new UIViewController to present the new page in a new Web View frame on top of the existing Web View. **Note:** This new window provides no controls, so it can only be closed with `window.close()` in JS.
1 parent c90fad4 commit 32b21e1

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ - (void)pluginInitialize
174174
self.CDV_ASSETS_URL = [NSString stringWithFormat:@"%@://%@", scheme, hostname];
175175
}
176176

177-
self.uiDelegate = [[CDVWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
177+
CDVWebViewUIDelegate* uiDelegate = [[CDVWebViewUIDelegate alloc] initWithTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
178+
uiDelegate.allowNewWindows = [settings cordovaBoolSettingForKey:@"AllowNewWindows" defaultValue:NO];
179+
self.uiDelegate = uiDelegate;
178180

179181
CDVWebViewWeakScriptMessageHandler *weakScriptMessageHandler = [[CDVWebViewWeakScriptMessageHandler alloc] initWithScriptMessageHandler:self];
180182

CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h

+4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
#import <WebKit/WebKit.h>
2121

2222
@interface CDVWebViewUIDelegate : NSObject <WKUIDelegate>
23+
{
24+
NSMutableArray<UIViewController*>* windows;
25+
}
2326

2427
@property (nonatomic, copy) NSString* title;
28+
@property (nonatomic, assign) BOOL allowNewWindows;
2529

2630
- (instancetype)initWithTitle:(NSString*)title;
2731

CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m

+40
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ - (instancetype)initWithTitle:(NSString*)title
2626
self = [super init];
2727
if (self) {
2828
self.title = title;
29+
windows = [[NSMutableArray alloc] init];
2930
}
3031

3132
return self;
@@ -120,4 +121,43 @@ - (void) webView:(WKWebView*)webView runJavaScriptTextInputPanelWithPrompt:
120121
[rootController presentViewController:alert animated:YES completion:nil];
121122
}
122123

124+
- (WKWebView*) webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures
125+
{
126+
if (!navigationAction.targetFrame.isMainFrame) {
127+
if (self.allowNewWindows) {
128+
WKWebView* v = [[WKWebView alloc] initWithFrame:webView.frame configuration:configuration];
129+
v.UIDelegate = webView.UIDelegate;
130+
v.navigationDelegate = webView.navigationDelegate;
131+
132+
UIViewController* vc = [[UIViewController alloc] init];
133+
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
134+
vc.view = v;
135+
136+
[windows addObject:vc];
137+
138+
UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
139+
[rootController presentViewController:vc animated:YES completion:nil];
140+
return v;
141+
} else {
142+
[webView loadRequest:navigationAction.request];
143+
}
144+
}
145+
146+
return nil;
147+
}
148+
149+
- (void)webViewDidClose:(WKWebView*)webView
150+
{
151+
for (UIViewController* vc in windows) {
152+
if (vc.view == webView) {
153+
[vc dismissViewControllerAnimated:YES completion:nil];
154+
[windows removeObject:vc];
155+
break;
156+
}
157+
}
158+
159+
// We do not allow closing the primary WebView
160+
}
161+
162+
123163
@end

0 commit comments

Comments
 (0)