【问题标题】:Sandbox & WKWebView loadFileURL(_, allowingReadAccessTo:) InconsistencySandbox & WKWebView loadFileURL(_, allowedReadAccessTo:) 不一致
【发布时间】:2017-12-28 23:59:49
【问题描述】:

在 os/x 上使用新的闪亮 WKWebView 和沙箱,需要一些干预重置或清除,因为后续调用加载文件 URL 将被忽略;这与WKWebView loadFileURL works only once 上的早期问题有些相关 - ios 那里,这里我在 os/X 上做

if loadURL.isFileURL {
    webView.loadFileURL(loadURL, allowingReadAccessTo: loadURL)
}
else
{
    webView.load(URLRequest(url: loadURL))
}

我尝试将 loadURL.deletingLastPathComponent() 作为第二个参数传递,但随后全部中断 - 没有加载文件 URL,也没有使用用户的主路径或整个根 'file:///',也没有“临时”异常重新: 绝对文件路径。最后,尝试中间的 topLoading() 没有任何影响。

加载后续文件 URL 的唯一解决方案(糟糕)是首先加载非文件 URL!

在沙盒环境中,这似乎会产生意想不到的后果?

【问题讨论】:

    标签: macos wkwebview appstore-sandbox


    【解决方案1】:

    好吧,这可行但很难看 - webView 子类函数,因为当先前加载文件 url 时,您无法重用 webView。此解决方法将实例化一个新窗口/文档来扔旧窗口 - 除非作为用户偏好,他们希望保留旧窗口(newWindows 标志为真):

    func loadNext(url: URL) {
        let doc = self.window?.windowController?.document as! Document
        let newWindows = UserSettings.createNewWindows.value
        var fileURL = url
    
        if !url.isFileURL {
            if newWindows {
                do
                {
                    let next = try NSDocumentController.shared().openUntitledDocumentAndDisplay(true) as! Document
                    let oldWindow = self.window
                    let newWindow = next.windowControllers.first?.window
                    (newWindow?.contentView?.subviews.first as! MyWebView).load(URLRequest(url: url))
                    newWindow?.offsetFromWindow(oldWindow!)
                }
                catch let error {
                    NSApp.presentError(error)
                    Swift.print("Yoink, unable to create new url doc for (\(url))")
                    return
                }
            }
            else
            {
                self.load(URLRequest(url: url))
            }
        }
    
        if let origURL = (fileURL as NSURL).resolvedFinderAlias() {
            fileURL = origURL
        }
    
        if appDelegate.isSandboxed() && !appDelegate.storeBookmark(url: fileURL) {
            Swift.print("Yoink, unable to sandbox \(fileURL))")
            return
        }
    
        if !(self.url?.isFileURL)! && !newWindows {
            self.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
            doc.update(to: fileURL, ofType: fileURL.pathExtension)
            return
        }
    
        //  We need or want a new window; if need, remove the old afterward
        do {
            let next = try NSDocumentController.shared().openUntitledDocumentAndDisplay(true) as! Document
            let oldWindow = doc.windowControllers.first?.window
            let newWindow = next.windowControllers.first?.window
            (newWindow?.contentView?.subviews.first as! MyWebView).loadFileURL(fileURL, allowingReadAccessTo: fileURL)
            if newWindows {
                newWindow?.offsetFromWindow(oldWindow!)
            }
            else
            {
                newWindow?.overlayWindow(oldWindow!)
                oldWindow?.orderOut(self)
            }
            next.update(to: fileURL, ofType: fileURL.pathExtension)
        }
        catch let error
        {
            NSApp.presentError(error)
            Swift.print("Yoink, unable to new doc (\(fileURL))")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 2019-03-03
      • 2018-04-18
      相关资源
      最近更新 更多