Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't silently ignore creation of new windows #885

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ - (void)pluginInitialize
self.CDV_ASSETS_URL = [NSString stringWithFormat:@"%@://%@", scheme, hostname];
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
#import <WebKit/WebKit.h>

@interface CDVWebViewUIDelegate : NSObject <WKUIDelegate>
{
NSMutableArray<UIViewController*>* windows;
}

@property (nonatomic, copy) NSString* title;
@property (nonatomic, assign) BOOL allowNewWindows;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ - (instancetype)initWithTitle:(NSString*)title
self = [super init];
if (self) {
self.title = title;
windows = [[NSMutableArray alloc] init];
}

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

- (WKWebView*) webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration forNavigationAction:(WKNavigationAction*)navigationAction windowFeatures:(WKWindowFeatures*)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
if (self.allowNewWindows) {
WKWebView* v = [[WKWebView alloc] initWithFrame:webView.frame configuration:configuration];
v.UIDelegate = webView.UIDelegate;
v.navigationDelegate = webView.navigationDelegate;

UIViewController* vc = [[UIViewController alloc] init];
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
vc.view = v;

[windows addObject:vc];

UIViewController* rootController = [UIApplication sharedApplication].delegate.window.rootViewController;
[rootController presentViewController:vc animated:YES completion:nil];
return v;
} else {
[webView loadRequest:navigationAction.request];
}
}

return nil;
}

- (void)webViewDidClose:(WKWebView*)webView
{
for (UIViewController* vc in windows) {
if (vc.view == webView) {
[vc dismissViewControllerAnimated:YES completion:nil];
[windows removeObject:vc];
break;
}
}

// We do not allow closing the primary WebView
}


@end