How can I fetch files from Google Drive in my iOS app using WKWebView?

I’m working on an iOS app with Swift and I’m stuck. I’m trying to get files from Google Drive using WKWebView, but nothing happens when I tap the download button. Here’s what I’ve done so far:

import UIKit
import WebKit

class MainViewController: UIViewController, WKNavigationDelegate, WKDownloadDelegate {
    
    var browserView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let config = WKWebViewConfiguration()
        browserView = WKWebView(frame: view.bounds, configuration: config)
        browserView.navigationDelegate = self
        browserView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(browserView)
        
        NSLayoutConstraint.activate([
            browserView.topAnchor.constraint(equalTo: view.topAnchor),
            browserView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            browserView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            browserView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        if let gdURL = URL(string: "GOOGLE_DRIVE_FILE_LINK") {
            let req = URLRequest(url: gdURL)
            browserView.load(req)
        }
    }
    
    func webView(_ webView: WKWebView, navigationResponse: WKNavigationResponse, didBecome download: WKDownload) {
        download.delegate = self
        print("Download started")
    }
    
    func download(_ download: WKDownload, decideDestinationUsing response: URLResponse, suggestedFilename: String, completionHandler: @escaping (URL?) -> Void) {
        let docsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let saveURL = docsPath.appendingPathComponent(suggestedFilename)
        
        completionHandler(saveURL)
        print("File will be saved at: \(saveURL)")
    }
    
    func downloadDidFinish(_ download: WKDownload) {
        print("Download finished")
    }
    
    func download(_ download: WKDownload, didFailWithError error: Error, resumeData: Data?) {
        print("Download error: \(error.localizedDescription)")
    }
}

Is this approach even possible? Should I try URLSession instead? Or maybe use the Google Drive API? Also, can I still download files using direct URLs? Any help would be great!

hey man, i feel ya. google drive can be a pain sometimes. i’d say ditch the WKWebView thing and go for the google drive api. it’s way easier to work with. you’ll need to set up OAuth, but it’s worth it. trust me, i’ve been there. once you get it going, downloading files will be a breeze. good luck with your app!

Hey there! I’ve been down this road before, and let me tell you, WKWebView can be a bit tricky with Google Drive. Here’s what worked for me:

Ditch the WKWebView approach and go straight for the Google Drive API. It’s a game-changer. You’ll need to set up OAuth 2.0, but it’s worth it. With the API, you get way more control over file access and downloads.

I remember banging my head against the wall trying to make WKWebView work with direct URLs. The issue? Google Drive’s authentication. It’s a pain to maintain through WKWebView.

If you’re dead set on using WKWebView, maybe try this: use it just for browsing and selecting files, then switch to the API for the actual download. It’s a bit of extra work, but it gives you the best of both worlds.

Trust me, once you get the API set up, you’ll wonder why you ever tried anything else. It’s smoother, more reliable, and you won’t have to deal with those frustrating ‘nothing happens’ moments. Good luck!

I’ve encountered similar issues when working with Google Drive and WKWebView. In my experience, using WKWebView for direct file downloads from Google Drive can be problematic due to authentication and security constraints.

Instead, I’d recommend using the Google Drive API. It provides more reliable and secure access to files. You’ll need to set up OAuth 2.0 for authentication, but once that’s done, you can use URLSession to download files directly.

If you’re set on using direct URLs, ensure you’re handling authentication properly. Google Drive often requires a valid session or token for file access, which may not be maintained through WKWebView.

Alternatively, consider using a hybrid approach: use WKWebView for file selection and the Google Drive API for actual downloads. This can provide a smoother user experience while maintaining robust file handling capabilities.