Using WebView in a modal NSWindow not working?

前端 未结 2 1005
轻奢々
轻奢々 2021-01-20 13:26

How should you use WebKit\'s WebView with a modal dialog?

[_webView setMainFrameURL:[NSString fromStdString:url]];
[_nsWindow makeKeyAndOrderFront:nil];
retu         


        
2条回答
  •  难免孤独
    2021-01-20 14:00

    WebView only works on the main loop and thus doesn't cooperate in this case. One solution would be to run the modal session yourself and keep the main loop manually alive (similar to what is proposed here). E.g.:

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

    Note that you probably want to use something like -runMode:beforeDate: instead to keep the CPU load down.

提交回复
热议问题