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 5dede305 fix: alerts could freeze the application (#1437)
5dede305 is described below

commit 5dede305c66cea5716e3c80a1c236fd5049ff521
Author: Darryl Pogue <dar...@dpogue.ca>
AuthorDate: Wed Aug 28 02:23:56 2024 -0700

    fix: alerts could freeze the application (#1437)
    
    Closes GH-1120.
    Closes GH-1121.
    Closes GH-1429.
---
 .../Plugins/CDVWebViewEngine/CDVWebViewEngine.m    |  3 +-
 .../CDVWebViewEngine/CDVWebViewUIDelegate.h        | 20 ++++++--
 .../CDVWebViewEngine/CDVWebViewUIDelegate.m        | 56 +++++++++++++---------
 3 files changed, 51 insertions(+), 28 deletions(-)

diff --git 
a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m 
b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m
index bae6838f..56b86186 100644
--- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m
+++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m
@@ -192,7 +192,8 @@
         self.CDV_ASSETS_URL = [NSString stringWithFormat:@"%@://%@", scheme, 
hostname];
     }
 
-    CDVWebViewUIDelegate* uiDelegate = [[CDVWebViewUIDelegate alloc] 
initWithTitle:[[NSBundle mainBundle] 
objectForInfoDictionaryKey:@"CFBundleDisplayName"]];
+    CDVWebViewUIDelegate* uiDelegate = [[CDVWebViewUIDelegate alloc] 
initWithViewController:vc];
+    uiDelegate.title = [[NSBundle mainBundle] 
objectForInfoDictionaryKey:@"CFBundleDisplayName"];
     uiDelegate.allowNewWindows = [settings 
cordovaBoolSettingForKey:@"AllowNewWindows" defaultValue:NO];
     self.uiDelegate = uiDelegate;
 
diff --git 
a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h 
b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h
index 8ae39b67..581eea0c 100644
--- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h
+++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.h
@@ -19,14 +19,24 @@
 
 #import <WebKit/WebKit.h>
 
+#ifdef NS_SWIFT_UI_ACTOR
+#define CDV_SWIFT_UI_ACTOR NS_SWIFT_UI_ACTOR
+#else
+#define CDV_SWIFT_UI_ACTOR
+#endif
+
+@class CDVViewController;
+
+NS_ASSUME_NONNULL_BEGIN
+
+CDV_SWIFT_UI_ACTOR
 @interface CDVWebViewUIDelegate : NSObject <WKUIDelegate>
-{
-    NSMutableArray<UIViewController*>* windows;
-}
 
-@property (nonatomic, copy) NSString* title;
+@property (nonatomic, nullable, copy) NSString* title;
 @property (nonatomic, assign) BOOL allowNewWindows;
 
-- (instancetype)initWithTitle:(NSString*)title;
+- (instancetype)initWithViewController:(CDVViewController*)vc;
 
 @end
+
+NS_ASSUME_NONNULL_END
diff --git 
a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m 
b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m
index 784af8df..e06e5452 100644
--- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m
+++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewUIDelegate.m
@@ -18,22 +18,32 @@
  */
 
 #import "CDVWebViewUIDelegate.h"
+#import <Cordova/CDVViewController.h>
+
+@interface CDVWebViewUIDelegate ()
+
+@property (nonatomic, weak) CDVViewController *viewController;
+
+@end
 
 @implementation CDVWebViewUIDelegate
+{
+    NSMutableArray<UIViewController *> *windows;
+}
 
-- (instancetype)initWithTitle:(NSString*)title
+- (instancetype)initWithViewController:(CDVViewController *)vc
 {
     self = [super init];
+
     if (self) {
-        self.title = title;
+        self.viewController = vc;
+        self.title = vc.title;
         windows = [[NSMutableArray alloc] init];
     }
-
     return self;
 }
 
-- (void)     webView:(WKWebView*)webView 
runJavaScriptAlertPanelWithMessage:(NSString*)message
-    initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void 
(^)(void))completionHandler
+- (void)webView:(WKWebView*)webView 
runJavaScriptAlertPanelWithMessage:(NSString*)message 
initiatedByFrame:(WKFrameInfo*)frame completionHandler:(CDV_SWIFT_UI_ACTOR void 
(^)(void))completionHandler
 {
     UIAlertController* alert = [UIAlertController 
alertControllerWithTitle:self.title
                                                                    
message:message
@@ -49,13 +59,10 @@
 
     [alert addAction:ok];
 
-    UIViewController* rootController = [UIApplication 
sharedApplication].delegate.window.rootViewController;
-
-    [rootController presentViewController:alert animated:YES completion:nil];
+    [[self topViewController] presentViewController:alert animated:YES 
completion:nil];
 }
 
-- (void)     webView:(WKWebView*)webView 
runJavaScriptConfirmPanelWithMessage:(NSString*)message
-    initiatedByFrame:(WKFrameInfo*)frame completionHandler:(void (^)(BOOL 
result))completionHandler
+- (void)webView:(WKWebView*)webView 
runJavaScriptConfirmPanelWithMessage:(NSString*)message 
initiatedByFrame:(WKFrameInfo*)frame completionHandler:(CDV_SWIFT_UI_ACTOR void 
(^)(BOOL result))completionHandler
 {
     UIAlertController* alert = [UIAlertController 
alertControllerWithTitle:self.title
                                                                    
message:message
@@ -80,14 +87,10 @@
         }];
     [alert addAction:cancel];
 
-    UIViewController* rootController = [UIApplication 
sharedApplication].delegate.window.rootViewController;
-
-    [rootController presentViewController:alert animated:YES completion:nil];
+    [[self topViewController] presentViewController:alert animated:YES 
completion:nil];
 }
 
-- (void)      webView:(WKWebView*)webView 
runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt
-          defaultText:(NSString*)defaultText 
initiatedByFrame:(WKFrameInfo*)frame
-    completionHandler:(void (^)(NSString* result))completionHandler
+- (void)webView:(WKWebView*)webView 
runJavaScriptTextInputPanelWithPrompt:(NSString*)prompt 
defaultText:(NSString*)defaultText initiatedByFrame:(WKFrameInfo*)frame 
completionHandler:(CDV_SWIFT_UI_ACTOR void (^)(NSString* 
result))completionHandler
 {
     UIAlertController* alert = [UIAlertController 
alertControllerWithTitle:self.title
                                                                    
message:prompt
@@ -116,12 +119,10 @@
         textField.text = defaultText;
     }];
 
-    UIViewController* rootController = [UIApplication 
sharedApplication].delegate.window.rootViewController;
-
-    [rootController presentViewController:alert animated:YES completion:nil];
+    [[self topViewController] presentViewController:alert animated:YES 
completion:nil];
 }
 
-- (WKWebView*) webView:(WKWebView*)webView 
createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration 
forNavigationAction:(WKNavigationAction*)navigationAction 
windowFeatures:(WKWindowFeatures*)windowFeatures
+- (nullable WKWebView*)webView:(WKWebView*)webView 
createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration 
forNavigationAction:(WKNavigationAction*)navigationAction 
windowFeatures:(WKWindowFeatures*)windowFeatures
 {
     if (!navigationAction.targetFrame.isMainFrame) {
         if (self.allowNewWindows) {
@@ -135,8 +136,7 @@
 
             [windows addObject:vc];
 
-            UIViewController* rootController = [UIApplication 
sharedApplication].delegate.window.rootViewController;
-            [rootController presentViewController:vc animated:YES 
completion:nil];
+            [[self topViewController] presentViewController:vc animated:YES 
completion:nil];
             return v;
         } else {
             [webView loadRequest:navigationAction.request];
@@ -159,5 +159,17 @@
     // We do not allow closing the primary WebView
 }
 
+#pragma mark - Utility Methods
+
+- (nullable UIViewController *)topViewController
+{
+    UIViewController *vc = self.viewController;
+
+    while (vc.presentedViewController != nil && ![vc.presentedViewController 
isBeingDismissed]) {
+        vc = vc.presentedViewController;
+    }
+
+    return vc;
+}
 
 @end


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

Reply via email to