提问人:sanjay 提问时间:9/29/2023 最后编辑:sanjay 更新时间:10/2/2023 访问量:104
如何在 macOS 应用程序中的 WKWebView 中打开查找器?
How to open finder inside WKWebView in macOS app?
问:
在 macOS 中,我们的要求是在 Web 视图中单击“上传文件”时打开 Finder。当我在Safari中打开相同的URL链接时,它可以工作,但是在macOS应用程序中Open Finder中,它没有被触发。
我使用了委托函数,但它没有被触发。WKWebKit
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;
我也试过,但没有运气。WKNavigationDelegate
webView:didStartProvisionalNavigation:
webView:didCommitNavigation:
webView:didFinishNavigation:
webView:didFailNavigation:withError:
webView:decidePolicyForNavigationAction:decisionHandler:
还有功能。WKUIDelegate
webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:
webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:
webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:
webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:
这是我尝试过的代码,
@interface ViewController() <WKUIDelegate, WKNavigationDelegate>
@property (nonatomic, assign) BOOL isFinderOpen;
@end
@implementation ViewController
(IBAction)buttonClick:(id)sender {
NSString *urlAddress = [NSString stringWithFormat:@"https://www.diawi.com/"];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlAddress]]];
}
- (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray<NSURL *> * _Nullable))completionHandler{
if (!self.isFinderOpen) {
self.isFinderOpen = YES;
[self.webView.configuration.preferences setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseFiles:YES];
[openPanel beginWithCompletionHandler:^(NSInteger result) {
self.isFinderOpen = NO;
if (result == NSModalResponseOK) {
NSURL *url = [openPanel URL];
if (url) {
NSLog(@"%@",url);
}
}
else if (result == NSModalResponseCancel) {
NSLog(@"nil");
}
}];
}
}
我错过了什么吗?
答: 暂无答案
评论
WKUIDelegate