RT,在OSX下的WebView中(非iOS),載入html網頁時,window.showModalDialog這句js無效,無法彈窗,在Safari中載入同樣網頁時有回應,請問有大神做過類似的問題麼?
程式碼如下:
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request{
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 300) styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:YES];
[window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
WebView *webView = [[WebView alloc] init];
[webView setFrameLoadDelegate:self];
window.contentView = webView;
[webView.mainFrame loadRequest:request];
[window makeKeyAndOrderFront:self];
return webView;
}
- (WebView *)webView:(WebView *)sender createWebViewModalDialogWithRequest:(NSURLRequest *)request{
return [self webView:sender createWebViewWithRequest:request];
}
- (void)webViewRunModal:(WebView *)sender{
[sender.window makeKeyAndOrderFront:self];
}
- (void)webViewShow:(WebView *)sender{
[sender.window makeKeyAndOrderFront:self];
}
- (void)webViewClose:(WebView *)sender{
[sender.window close];
}
给我你的怀抱2017-05-02 09:25:47
最好不要用showModalDialog,換成p比較好,osx的webview我沒用過,但是以前做android開發的時候,webview是不支持模態窗口的彈出的,換成p才行,不知道osx的webview不彈出是不是這個原因
黄舟2017-05-02 09:25:47
調試了很久終於解決了,提供一下程式碼給後來人一個思路。
#pragma -mark -WebView Delegate
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request{
NSUInteger windowStyleMask = NSClosableWindowMask|NSMiniaturizableWindowMask|NSTitledWindowMask;
_webWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 300) styleMask:windowStyleMask backing:NSBackingStoreNonretained defer:YES];
[_webWindow setReleasedWhenClosed:NO];
WebView* newWebView = [[WebView alloc] initWithFrame:[_webWindow contentRectForFrameRect:_webWindow.frame]];
[newWebView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[[newWebView mainFrame] loadRequest:request];
[_webWindow setContentView:newWebView];
[_webWindow center];
[_webWindow makeKeyAndOrderFront:nil];
return newWebView;
}
- (WebView *)webView:(WebView *)sender createWebViewModalDialogWithRequest:(NSURLRequest *)request{
return [self webView:sender createWebViewWithRequest:request];
}
- (void)webViewRunModal:(WebView *)sender{
[sender.window makeKeyAndOrderFront:self];
}
- (void)webViewShow:(WebView *)sender{
[sender.window makeKeyAndOrderFront:self];
}
- (void)webViewClose:(WebView *)sender{
[sender.window close];
}