Search code examples
ioscordovahookalertinappbrowser

alert() not displayed from Cordova InAppBrowser event listener - Conflict w/ Cordova window.alert hook


Using Cordova CLI version 5.4.1, Platform is iOS, Running on iOS emulator

From my app's onDeviceReady handler, I am calling a function to set a hook for alerts to be shown using the native dialog as follows:

overrideBrowserAlert: function() {
    if (navigator.notification) { // Override default HTML alert with native dialog
        window.alert = function (message) {
            navigator.notification.alert(
                message,    // message
                null,       // callback
                "MyAppTitle", // title
                'OK'        // buttonName
            );
        };
    }
},

Most alert calls work, but the following doesn't.

At some stage I am opening an InAppBrowser window. It closes successfully when the "Done" button is clicked. I have registered an event listener for the event exit which does two things:

  1. alert("InAppBrowser window closed");
  2. Make an AJAX call, get some HTML from a remote server and show it in a <div> element

HERE IS THE PROBLEM: The AJAX call is successful, but the alert IS NOT DISPLAYED. This means the exit event is fired successfully and there is a problem with the alert call only.

THE PROBLEM GOES AWAY LIKE THIS: Guessing that it might be a scope or event handling conflict, I commented out the code for the window.alert hook (i.e. the overrideBrowserAlert call above), and sure enough, the alert in the InAppBrowser exit event handler works.

MY QUESTION IS: Does anyone know how to resolve this conflict so that the alert in the InAppBrowser exit event handler works AND the window.alert hook is also enabled?

Thanks!


Solution

  • This is a bug on the cordova-plugin-inappbrowser

    I've filled an issue https://issues.apache.org/jira/browse/CB-10855

    You can fix it by changing the close method to

    - (void)close
    {
        [CDVUserAgentUtil releaseLock:&_userAgentLockToken];
        self.currentURL = nil;
    
        __weak CDVInAppBrowserViewController* weakSelf = self;
    
        // Run later to avoid the "took a long time" log message.
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([weakSelf respondsToSelector:@selector(presentingViewController)]) {
                [[weakSelf presentingViewController] dismissViewControllerAnimated:YES completion:^{
                    if ((weakSelf.navigationDelegate != nil) && [weakSelf.navigationDelegate respondsToSelector:@selector(browserExit)]) {
                        [weakSelf.navigationDelegate browserExit];
                    }
                }];
            } else {
                [[weakSelf parentViewController] dismissViewControllerAnimated:YES completion:nil];
            }
        });
    }