Ich dachte, ich sollte eine klare Antwort für diejenigen geben, die nicht verstehen, wie eine herunterladbare Datei überprüfen
Hier ist, wie:
Sie ein Array erstellen irgendwo, dass die unterstützten Dateitypen mime hat
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"]
Dann in dieser UIWebViewDelegate Methode, die Sie tun dies
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
}
}
Klingt so, als ob Sie eine 'HEAD' Anfrage an die URL senden und sehen möchten, ob Sie eine 200 oder eine 404 bekommen. Sie können es mit NSURLSession machen. –
Danke das war viel einfacher als ich dachte. – Raffi