我已经实现了一个名为RCHDownload
它具有使用String
开始下载文件的功能,它被称为addDownload(url: String)
使用名为sharedInstance
现在我想做的是在我的UIWebView
委托方法
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {}
我希望它在开始下载之前检查可能的文件,但到目前为止我无法做到这一点
我尝试使用以下内容进行检查:request.url.isFileURL
& request.url.checkResourceIsReachable()
没有运气。有什么建议吗?
答案 0 :(得分:0)
我认为我应该为那些不了解如何检查可下载文件的人提供一个明确的答案
以下是:
您可以在某处创建一个包含受支持文件的数组mime Types
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
}
}