WebView 中的插件对象被破坏?

发布于 2024-10-22 06:12:39 字数 397 浏览 2 评论 0原文

这是我的情况:

我编写了一个简单的网络浏览器。一旦我了解了概念并弄清楚如何在特定选项卡上执行操作,选项卡式浏览就很容易开始工作。它运行良好,并且在大多数情况下都非常可靠。

然而,有一个问题一直困扰着我,我无法找出其原因。

假设我打开一个新选项卡并导航到 YouTube。我点击一个视频,Flash 播放器就会加载。视频播放并且一切正常。我现在创建另一个新选项卡并导航到某个网站。 YouTube 播放器的音频完全停止。

当我切换回 YouTube 选项卡时,页面仍然会原样存在,只是播放器必须完全重新加载,就像我刚刚重新加载页面一样。这似乎也适用于其他插件类型。

这是什么原因造成的?

垃圾收集未启用,据我所知,我正在正确创建网络视图。我是否在整个过程中错过了一些愚蠢而简单的小事情?

Here's my situation:

I've written a simple web browser. Tabbed browsing was easy enough to get working once I wrapped my head around the concepts and figured how to perform operations on specific tabs. It works well and is pretty reliable under most circumstances.

A problem has plagued me, however, and I cannot figure out its cause.

Let's say I open a new tab and navigate to YouTube. I click on a video and the flash player loads. The video plays and all works fine. I now create another new tab and navigate to some site. The audio from the youtube player stops completely.

When I switch back to the youtube tab, the page will all still be there just as it was except the player has to reload completely, as if I had just reloaded the page. This seems to apply to other plugin types as well.

What's causing this?

Garbage collection is not enabled and as far as I know I'm creating the web views properly. Is there some silly, simple little thing that I missed somewhere along the line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦慕雪 2024-10-29 06:12:39

WebView 上的 - (void)setHostWindow:(NSWindow *)hostWindow 方法可能就是您正在寻找的。

每当封闭窗口的样式掩码发生更改时,我都会在 WebView 重新加载中遇到 Flash 相同的问题。包装对 setStyleMask 的调用解决了该问题,如下所示:

NSWindow *hostWindow = [[NSWindow alloc] init];
[self.webView setHostWindow:hostWindow];
[[self windowForSheet] setStyleMask:styleMask];
[self.webView setHostWindow:nil];
[hostWindow release];

该方法的文档并不出色,但它确实明确指出内部有一个 WebView NSTabView 作为用例之一:

该方法将接收者的宿主窗口设置为hostWindow。仅当要暂时从其窗口中删除 Web 视图并且您希望 Web 视图继续运行(例如,您不想中断正在进行的加载)时,您的应用程序才应使用此方法。由于接收者保留了 hostWindow,因此您有责任在关闭窗口之前将主机窗口设置为 nil,以避免出现保留循环。

例如,如果将 Web 视图附加到 NSTabView 对象(如在选项卡式浏览器实现中),则可以调用此方法。当视图不在活动选项卡中时,NSTabView 对象会将视图移出窗口,因此您需要在 Web 视图从窗口中删除之前调用此方法。如果您不调用此方法,当 Web 视图从其窗口中删除时,插件将停止运行。

The - (void)setHostWindow:(NSWindow *)hostWindow method on WebView is probably what you're looking for.

I had the same problem with the flash in a WebView reloading whenever the syle mask of the enclosing window was changed. Wrapping the call to setStyleMask fixed the problem, as follows:

NSWindow *hostWindow = [[NSWindow alloc] init];
[self.webView setHostWindow:hostWindow];
[[self windowForSheet] setStyleMask:styleMask];
[self.webView setHostWindow:nil];
[hostWindow release];

The documentation for the method isn't stellar, but it does explicitly state a having a WebView inside a NSTabView as one of the use cases:

This method sets the receiver’s host window to hostWindow. Your application should only use this method if a web view is going to be removed from its window temporarily, and you want the web view to continue operating (for example, you don’t want to interrupt a load in progress). Since the receiver retains hostWindow, it is your responsibility to set the host window to nil before closing the window to avoid a retain loop.

For example, you might invoke this method if you attach a web view to an NSTabView object (as in a tabbed browser implementation). The NSTabView object takes views out of the window when they are not in the active tab, so you need to invoke this method before the web view is removed from its window. If you don't invoke this method, plug-ins will stop operating when the web view is removed from its window.

耳根太软 2024-10-29 06:12:39

我遇到了类似的问题,但有一个带有 webView 的窗口已关闭并恢复。不幸的是@mlwelles 的解决方案并没有单独解决问题。

然而,解决的办法是在关闭之前从窗口中删除 webView(正确的“时机”很重要)。我想出的是这样的:

id contentView;
id tmpHostWindow;
[window setDelegate:self];

- (BOOL)windowShouldClose:(NSNotification *)notification
{
    // set temporary hostWindow on WebView and remove it from
    // the closed window to prevent stopping flash plugin
    // (windowWillClose would be better but that doesn't always work)

    tmpHostWindow = [[NSWindow alloc] init];
    [webView setHostWindow:tmpHostWindow];
    [window setContentView:nil];
    [contentView removeFromSuperview];

    return TRUE;
}

- (void)windowDidBecomeKey:(NSNotification *)notification
{
    // restore "hidden" webview
    // (would be better to do it in applicationShouldHandleReopen
    // but that seems to be too early (has no effect)

    if ([window contentView] != contentView) {
        [window setContentView:contentView];
        [webView setHostWindow:nil];
        tmpHostWindow = nil;
    }
}

I had a similar problem but with a window with a webView that is closed and restored. Unfortunately the solution by @mlwelles did not solve the problem alone.

What did solve however is removing the webView from the window before it closes (proper "timing" is important). What I came up with is something like this:

id contentView;
id tmpHostWindow;
[window setDelegate:self];

- (BOOL)windowShouldClose:(NSNotification *)notification
{
    // set temporary hostWindow on WebView and remove it from
    // the closed window to prevent stopping flash plugin
    // (windowWillClose would be better but that doesn't always work)

    tmpHostWindow = [[NSWindow alloc] init];
    [webView setHostWindow:tmpHostWindow];
    [window setContentView:nil];
    [contentView removeFromSuperview];

    return TRUE;
}

- (void)windowDidBecomeKey:(NSNotification *)notification
{
    // restore "hidden" webview
    // (would be better to do it in applicationShouldHandleReopen
    // but that seems to be too early (has no effect)

    if ([window contentView] != contentView) {
        [window setContentView:contentView];
        [webView setHostWindow:nil];
        tmpHostWindow = nil;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文