如何检查 UIWebView url link 可能的文件?
How to check UIWebView url link for possible file?
我已经实施了一个名为 RCHDownload
的模型 class
它具有使用 String
开始下载文件的功能,它被称为 addDownload(url: String)
使用名为 sharedInstance
的单例,我可以在任何地方使用此函数
现在我想做的是在我的 UIWebView
委托方法中
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {}
我希望它在开始下载之前检查可能的文件,但我目前无法做到这一点
我尝试使用 request.url.isFileURL
& request.url.checkResourceIsReachable()
检查,但没有成功。有什么建议吗?
我想我应该给那些不明白如何检查可下载文件的人一个明确的答案
方法如下:
您在某个地方创建了一个包含您支持的文件 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
}
}
我已经实施了一个名为 RCHDownload
它具有使用 String
开始下载文件的功能,它被称为 addDownload(url: String)
使用名为 sharedInstance
现在我想做的是在我的 UIWebView
委托方法中
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {}
我希望它在开始下载之前检查可能的文件,但我目前无法做到这一点
我尝试使用 request.url.isFileURL
& request.url.checkResourceIsReachable()
检查,但没有成功。有什么建议吗?
我想我应该给那些不明白如何检查可下载文件的人一个明确的答案
方法如下:
您在某个地方创建了一个包含您支持的文件 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
}
}