Cocoa topics: the case of the modal WebView

I want to include the occasional programming topic in the Dejal blog, when I encounter something that may be of interest to other Mac developers. Here's my first one.

A while back, I added a Check for Updates... window that displays a WebView of release notes, much like on my site. It works rather well. However, if a beta release has expired, I wanted it to display it modally. But for some strange reason the WebView didn't load when the window was run modally. I couldn't find a solution at the time, so I just had it redirect to the website.

I came across this issue again tonight, while adding Services support to BlogAssist. I'm introducing a handy new feature where you can just hit < in any app to display a modal panel like the existing floating window. It includes a WebView to preview how the marked-up HTML will appear. I really didn't want to have to sacrifice that.

So I found a solution: tickle the runloop! It turns out that the WebView will only work on the main loop. So rather than just call -runModalForWindow:, I use the more verbose -beginModalSessionForWindow: / -runModalSession: / -endModalSession: loop. And the key to keeping the WebView happy: call -limitDateForMode: each time around the loop, so the main loop keeps on truckin'.

Here's the code:

    NSModalSession session = [NSApp beginModalSessionForWindow:[self window]];
    int result = NSRunContinuesResponse;
   
    // Loop until some result other than continues:
    while (result == NSRunContinuesResponse)
    {
        // Run the window modally until there are no events to process:
        result = [NSApp runModalSession:session];
       
        // Give the main loop some time:
        [[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];
    }
   
    [NSApp endModalSession:session];

I hope this helps others with this dilemma.

Webview

Wow...this is kinda crazy.

I was working on a program that used core data and webview, but the webview would never display what I wanted it to...I looked into this modal stuff, but have still not found the answer.

Oh well,
Good job on your solution

Is there any way to stop the

Is there any way to stop the display or repositioning of the window? I've tried setting the content size to zero, so nothing appears, but if I do this I can never get the window to appear again. Even if I do this:

NSModalSession session = [NSApp beginModalSessionForWindow: window];
[window orderOut: self];

I can still see the window flicker.

------
dedicated server

David Sinclair's picture

Re: Is there any way to stop...

I'm not sure what you're trying to achieve. It doesn't make sense to run a window modally when it isn't visible.

If the window is appearing before you want it to, simply uncheck the option in IB for initial visibility. You can then show it when you want it.