JavaScript alert()无法在嵌入式WebView中使用

Geo*_*che 13 macos cocoa objective-c webview

当嵌入WebView在应用程序和加载HTML的页面的话,JavaScript的alert()/ confirm()的/ etc.不工作.

在文档中查看,没有相关的设置WebPreferences- 唯一看起来相关的是WebUIDelegates -(void)webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:等...但实现这些意味着为这些创建自定义对话框似乎非常多余...
我不需要自定义WebUIDelegate和我想继续使用默认的.

肯定有一些方法可以简单地启用alert()et al,但是如何?

小智 12

以下是执行基本工作的示例代码.但是,您需要确保将此对象注册为WebView的UIDelegate.

- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:message];
    [alert runModal];
    [alert release];
}
Run Code Online (Sandbox Code Playgroud)


Geo*_*che 5

事实证明,根本没有默认WebUIDelegate设置 - Apple似乎希望每个人都为自己实现相同的基本功能.

  • 但是js`prompt`有效.所以它有点不一致. (2认同)