You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by dp...@apache.org on 2020/06/18 15:16:43 UTC

[cordova-ios] branch master updated: Don't silently ignore creation of new windows (#885)

This is an automated email from the ASF dual-hosted git repository.

dpogue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-ios.git


The following commit(s) were added to refs/heads/master by this push:
     new 32b21e1  Don't silently ignore creation of new windows (#885)
32b21e1 is described below

commit 32b21e178b1aaa974284521fbebb79f4ac6d869c
Author: Darryl Pogue <da...@dpogue.ca>
AuthorDate: Thu Jun 18 08:16:15 2020 -0700

    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.
---
 .../Plugins/CDVWebViewEngine/CDVWebViewEngine.m    |  4 ++-
 .../CDVWebViewEngine/CDVWebViewUIDelegate.h        |  4 +++
 .../CDVWebViewEngine/CDVWebViewUIDelegate.m        | 40 ++++++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m
index cede8bc..6fd389e 100644
--- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m
+++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m
@@ -174,7 +174,9 @@
         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];
 
diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h
index 3a71e54..8ae39b6 100644
--- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h
+++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h
@@ -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;
 
diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m
index 8364637..784af8d 100644
--- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m
+++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m
@@ -26,6 +26,7 @@
     self = [super init];
     if (self) {
         self.title = title;
+        windows = [[NSMutableArray alloc] init];
     }
 
     return self;
@@ -120,4 +121,43 @@
     [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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org