Get a bundle and save a bundle! Click here to get Caboodle and 10 other great apps for only $49.95.
Also check out other fine Dejal products for Mac and iPhone, and Princess Padmé will be pleased.

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.

Comments

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