【问题标题】:How to check UIWebView url link for possible file?如何检查可能文件的 UIWebView url 链接?
【发布时间】:2016-06-21 00:16:33
【问题描述】:

我已经实现了一个名为RCHDownload的模型类

它有一个使用String开始下载文件的功能,它被称为addDownload(url: String)

这个函数可以在任何我想使用的地方使用一个叫做sharedInstance的单例

现在我要做的是在我的 UIWebView 委托方法中

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {}

我希望它在开始下载之前检查可能的文件,但到目前为止我无法做到这一点

我尝试使用:request.url.isFileURL & request.url.checkResourceIsReachable() 进行检查,但没有成功。有什么建议吗?

【问题讨论】:

  • 听起来你想向 URL 发送一个HEAD 请求,看看你得到的是 200 还是 404。你可以用 NSURLSession 来做。
  • 谢谢,这比我想象的要容易得多。

标签: ios objective-c swift uiwebview


【解决方案1】:

我想我应该给那些不明白如何检查可下载文件的人一个明确的答案

方法如下:

您在某处创建一个数组,其中包含您支持的文件 mime 类型

lazy var supportedFileTypes = ["audio/mpeg", "audio/aiff", "audio/x-aiff", "video/avi", "video/msvideo", "video/x-msvideo", "application/x-troff-msvideo", "text/plain", "application/octet-stream", "image/gif", "application/vnd.ms-excel", "application/excel", "application/x-excel", "application/xml", "text/xml", "application/zip", "multipart/x-zip", "image/bmp", "image/jpeg", "image/pjpeg", "application/x-compressed", "application/x-gzip", "text/x-h", "audio/x-mpequrl", "application/x-midi", "audio/x-midi", "audio/mpeg3", "audio/x-mpeg-3", "video/mpeg", "video/x-mpeg", "image/png", "application/mspowerpoint", "application/vnd.ms-powerpoint", "application/rtf", "application/x-rtf", "application/plain", "application/x-compressed", "application/gnutar", "audio/wav", "audio/x-wav", "image/tiff", "image/x-tiff", "application/msword", "video/quicktime", "application/x-7z-compressed", "audio/x-aac", "audio/mp4", "video/jpeg", "video/jpm", "video/3gpp", "video/3gpp2", "video/mp4"]

然后在这个 UIWebViewDelegate 方法中你这样做

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    let nvc = self.tabBarController?.viewControllers?[1] as! UINavigationController
    let dvc = nvc.viewControllers[0] as! RCHDownloadTVC
    var isDownloadable = false // Bool to check whether the file is downloadable

    // create a session with a http head request 
    let session = URLSession(configuration: URLSessionConfiguration.default())
    var newRequest = URLRequest(url: request.url!)
    newRequest.httpMethod = "HEAD"

    // Start a data task
    let task = session.dataTask(with: newRequest) { (data, response, error) in
        // It is important to use this code on main thread if you have UIAlertController to ask if the user wants to download the file
        DispatchQueue.main.sync(execute: {

            let httpResponse = response as? HTTPURLResponse
            // You check if there is a response - 200 means there is
            if httpResponse?.statusCode == 200 {
                // then you check for the mime type if it is supported by your application
                for (_, mime) in sharedManagerStore.supportedFileTypes.enumerated() {
                    if response?.mimeType == mime {
                        self.showDownloadDecisionAlert(with: { (alert) in
                            sharedDownloadStore.addDownload(with: (request.url?.absoluteString)!)
                            dvc.reloadDownloadController()
                            self.webView.mediaPlaybackRequiresUserAction = true
                            self.webView.allowsInlineMediaPlayback = false
                            self.webView.goBack()
                            isDownloadable = true
                            }, completionHandlerTwo: { (alert) in
                                self.webView.mediaPlaybackRequiresUserAction = false
                                self.webView.allowsInlineMediaPlayback = true
                                isDownloadable = false
                        })
                        break
                    }
                }
            }
        })
    }

    task.resume()

    if isDownloadable != true {
        // You return true if the file is not downloadable
        return true
    } else {
        // You return false if the file is downloadable
        return false
    }
}

【讨论】:

  • 您的代码是否适用于 youtube 抓取视频?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
相关资源
最近更新 更多