admin管理员组

文章数量:1122832

I am creating an iOS application in Swift. Downloading does not start when the “Download” button is pressed.

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKDownloadDelegate {
    
    var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let configuration = WKWebViewConfiguration()
        webView = WKWebView(frame: self.view.bounds, configuration: configuration)
        webView.navigationDelegate = self
        webView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(webView)
        
        NSLayoutConstraint.activate([
            webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            webView.topAnchor.constraint(equalTo: view.topAnchor),
            webView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        if let driveURL = URL(string: "GOOGLEDRIVE_FILE_URL") {
            let request = URLRequest(url: driveURL)
            webView.load(request)
        }
    }
    
    func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) {
        download.delegate = self
        print("download has started")
    }
    
    func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {
        let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let destinationURL = documentsPath.appendingPathComponent(suggestedFilename)
        
        completionHandler(destinationURL)
        print("file save location: \(destinationURL)")
    }
    
    func downloadDidFinish(_ download: WKDownload) {
        print("Download is complete")
    }
    
    func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) {
        print("download error: \(error.localizedDescription)")
    }
}

If it seems impossible, I am thinking of using URLSession to get it or using the API. Also, is it still ok to download by direct URL?

本文标签: iosI want to download files stored in Google Drive on WKWebViewStack Overflow