【问题标题】:Setting "Disable web security" and "allow file access from files" in iOS WKWebView在 iOS WKWebView 中设置“禁用网络安全”和“允许从文件访问文件”
【发布时间】:2016-07-01 00:46:37
【问题描述】:

如何在 iOS WKWebView 中禁用网络安全?我在 mac 系统中使用了命令“open /Applications/Google\ Chrome.app --args --disable-web-security --allow-file-access-from-files”打开镀铬。但是如何在 WKWebView 中做到这一点?谢谢!

【问题讨论】:

    标签: ios objective-c swift wkwebview


    【解决方案1】:

    无法在 WKWebView 中禁用网络安全 - 没有偏好 - 请参阅 iOS source code for WebKit preferences

    有一种方法可以允许从文件 URL 进行访问,尽管它不受官方支持。在source code 中存在首选项,因此您可以这样设置:

    [wkWebView.configuration.preferences setValue:@TRUE forKey:@"allowFileAccessFromFileURLs"];
    

    这允许访问相对于内容 src 的文件 URL。例如如果本地页面是foo/bar/index.html,您可以访问foo/bar/ 中的文件(例如foo/bar/1.jpgfoo/bar/sub/2.jpg),但不能访问外部(例如foo/other/3.jpgDocuments/NoCloud/4.jpg)。

    【讨论】:

    • 你知道这是否会被 appStore 接受吗?
    • 我不明白为什么不这样做。如果可以禁用网络安全,我很确定它会被拒绝,但这只是为了启用本地文件访问。但是,只有一种方法可以找出答案:提交更多应用审查,看看会发生什么:-)
    • 这里是快速版本:wkWebView?.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs");
    • 我有许多应用程序使用相同的设置,都从苹果接受。
    • 请在此处评论任何因此问题而被拒绝的应用程序。
    【解决方案2】:

    您还可以将自定义 URL 方案处理程序添加到 WKWebView 并在您的网页中使用该方案。然后,此自定义方案处理程序可以从任意位置加载数据。需要注意的是,这会将整个文件加载到内存中,因此它不适用于大文件。

    let configuration = WKWebViewConfiguration()
    configuration.setURLSchemeHandler(MySchemeHandler(), forURLScheme: "MyScheme")
    let webView = WKWebView(frame: .zero, configuration: configuration)
    

    您的 MySchemeHandler 需要实现 webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask)webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask)(尽管后者可以为空)。然后,将自定义 URL 转换为文件 URL 并加载文件。

    加载文件似乎很痛苦。可能有更好的方法,但这是我们使用的:

    URLSession.shared.dataTask(with: fileUrl) { data, response, error in
        if error != nil {
            // cancel
            return
        }
        let taskResponse: URLResponse
        if #available(iOS 13, *) {
            taskResponse = HTTPURLResponse(url: urlSchemeTask.request.url!, mimeType: response?.mimeType, expectedContentLength: Int(response?.expectedContentLength ?? 0), textEncodingName: response?.textEncodingName)
        } else {
            // The HTTPURLResponse object created above using the URLResponse constructor crashes when sent to
            // urlSchemeTask.didReceive below in iOS 12. I have no idea why that is, but it DOESN'T crash if we
            // instead use the HTTPURLResponse-only constructor. Similarly, it doesn't crash if we create a
            // URLResponse object instead of an HTTPURLResponse object. So, if we know the mimeType, construct an
            // HTTPURLResponse using the HTTPURLResponse constructor, and add the appropriate header field for that
            // mime type. If we don't know the mime type, construct a URLResponse instead.
            if let mimeType = response?.mimeType {
                // The imodeljs code that loads approximateTerrainHeights.json requires the HTTP Content-Type header
                // to be present and accurate. URLResponse doesn't have any headers. I have no idea how the
                // HTTPURLResponse contstructor could fail, but just in case it does, we fall back to the
                // URLResponse object.
                taskResponse = HTTPURLResponse(url: urlSchemeTask.request.url!, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: ["Content-Type": "\(mimeType); charset=UTF-8"]) ?? URLResponse(url: urlSchemeTask.request.url!, mimeType: response?.mimeType, expectedContentLength: Int(response?.expectedContentLength ?? 0), textEncodingName: response?.textEncodingName)
            } else {
                taskResponse = URLResponse(url: urlSchemeTask.request.url!, mimeType: response?.mimeType, expectedContentLength: Int(response?.expectedContentLength ?? 0), textEncodingName: response?.textEncodingName)
            }
        }
        urlSchemeTask.didReceive(taskResponse)
        urlSchemeTask.didReceive(data!)
        urlSchemeTask.didFinish()
    }.resume()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-26
      • 2020-10-05
      • 2010-10-09
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多