Using WebView in modal NSWindow not working?

How to use WebKit WebView with modal dialog?

[_webView setMainFrameURL:[NSString fromStdString:url]];
[_nsWindow makeKeyAndOrderFront:nil];
return [NSApp runModalForWindow:_nsWindow];

The previous code only works on Mac OS 10.6. Using 10.5, this is not moving to the specified URL. Without runModalForWindow, everything works.

+3
source share
2 answers

WebViewonly works in the main loop and thus does not interact in this case. One solution would be to start the modal session on your own and keep the main loop manually alive (similar to what is suggested here ). For instance:.

NSModalSession session = [NSApp beginModalSessionForWindow:yourWindow];
int result = NSRunContinuesResponse;

// Loop until some result other than continues:
while (result == NSRunContinuesResponse)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // 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];

    // Drain pool to avoid memory getting clogged:
    [pool drain];
}

[NSApp endModalSession:session];

, , , - -runMode:beforeDate: , .

+8

, WebView , . -runMode:beforeDate WebView :

- (void) OpenURL:(const char *)_url
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [NSApplication sharedApplication];
    [NSApp setDelegate:self];

    NSString *url = [NSString stringWithUTF8String:_url];

    NSLog(@"OpenURL: %@", url);

    NSRect windowRect = NSMakeRect(10.0f, 10.0f, 800.0f, 600.0f);

    NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect 
        styleMask:(NSResizableWindowMask|NSClosableWindowMask|NSTitledWindowMask) 
        backing:NSBackingStoreBuffered defer:NO];
    [window setDelegate:self];

    WebView *webview = [[WebView alloc] initWithFrame:windowRect 
        frameName:@"mainFrame" 
        groupName:nil];
    [webview setFrameLoadDelegate:self];
    [[webview mainFrame] 
          loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

    [window setContentView:webview];
    [window makeKeyAndOrderFront:nil];

    // Modal Session

    NSModalSession session = [NSApp beginModalSessionForWindow:window];
    _result = NSModalResponseContinue;

    while (_result == NSModalResponseContinue) {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        _result = [NSApp runModalSession:session];

        // The event loop is a runloop data source, so any ui event will 
        // wake up the source and make this method returns, and so 
        // you can block the run loop and tell him to wait that
        // something append. [2]
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                    beforeDate:[NSDate distantFuture]];

        [self doSomeWork];

        [pool drain];
    }
    [NSApp endModalSession:session];

    [pool release];
}

[NSApp stopModal], [NSApp abortModal] [NSApp stopModalWithCode:yourReturnCode] - :

- (void)windowWillClose:(NSNotification *)notification
{
    NSLog(@"windowWillClose");
    [NSApp stopModal];
}

:

+1

Source: https://habr.com/ru/post/1774506/


All Articles