代码之家  ›  专栏  ›  技术社区  ›  Oday

如何检测在UIWebView中点击文件链接?

  •  0
  • Oday  · 技术社区  · 8 年前

    我正在使用 UIWebView 在我的应用程序中,我想检测用户何时单击指向文件(pdf、doc、docx…)而不是另一个HTML页面的链接。 这将允许我下载文件并通过显示选项菜单打开它。

    我尝试使用以下功能:

    webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest,  navigationType: UIWebViewNavigationType) -> Bool
    

    content-type 如果不包含 text/html 我下载它并显示选项菜单。

    内容类型 .

    以前有人遇到过这个问题吗?我该怎么解决?

    我试图在互联网上搜索,但找不到任何类似的问题

    2 回复  |  直到 8 年前
        1
  •  0
  •   Kamlesh Shingarakhiya    8 年前

    试试这个

    override func viewDidLoad() {
            super.viewDidLoad()
            self.myWebview.delegate = self;
    
            // Do any additional setup after loading the view, typically from a nib.
            let url = URL(string: "YOUR_URL_STRING")
            debugPrint(url!)
            myWebview.loadRequest(URLRequest(url: url!))
        }
    
        func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
            debugPrint("func myWebView has been called")
            debugPrint(request.url!)
            if navigationType == UIWebViewNavigationType.linkClicked {
                if (request.url!.host! == "stackoverflow.com"){
                    return true
                } else {
                    //UIApplication.sharedApplication().openURL(request.URL!)
                    UIApplication.shared.open(request.url!)
                    return false
                }
            }
            return true
        }
    

    希望这对你有帮助。

        2
  •  0
  •   Oday    8 年前

       var checkingProcesses: [URL:CheckingProcess] = [:]
    
       func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    
        Logger.log("WebviewViewController", "shouldStartLoadWith", "with url: \(String(describing: request.url?.absoluteString))")
        self.loadingViewLogic(hide: false)
    
        if let currentURL = request.url {
    
            if let process = self.checkingProcesses[currentURL]{
    
                Logger.log("WebviewViewController", "shouldStartLoadWith", "Known process: \(String(describing: process))")
    
                if(process.status == CheckingStatus.finished){
    
                    if(process.filePath != nil){
                        self.openFileByPresentingOptions(pathURL: process.filePath!)
                        return false
    
                    }else if(process.errorMessage != nil){
    
                        //error
                        Util.showAlert(context: self, title: "Error getting URL type", message: process.errorMessage!, alertActions: nil)
                        process.clearResults()//In order to try the next time the user asks.
                        self.loadingViewLogic(hide: true)
                        return false
    
                    }else{
    
                        //link - open it
                        return true
                    }
    
                }else if(process.status == CheckingStatus.processing){
    
                    Util.showAlert(context: self, title: "Processing...", message: "Please wait...", alertActions: nil)
                    return false
    
                }else{//inactive
    
                    self.checkingProcesses[currentURL]?.startCheck()
                    return false
                }
    
            }else{// did not create the process yet
    
                Logger.log("WebviewViewController", "shouldStartLoadWith", "Creating new process for URL: \(currentURL.absoluteString)")
    
                self.checkingProcesses[currentURL] = CheckingProcess(url: currentURL, webView: self.webView)
                self.checkingProcesses[currentURL]?.startCheck()
                return false
            }
    
        }else{
    
            Logger.log("WebviewViewController", "shouldStartLoadWith", "Couldn't get the currentURL")
            self.loadingViewLogic(hide: true)
            return false
        }
    
    }
    

    我创建了一个类“CheckingProcess.swift”:

    import UIKit
    
    public enum CheckingStatus {
        case inactive
        case processing
        case finished
    }
    
    class CheckingProcess: CustomStringConvertible {
    
        var status : CheckingStatus;
        var url: URL;
        var filePath: URL? = nil;
        var errorMessage: String? = nil;
        let webView: UIWebView?
    
        init(url: URL, webView: UIWebView) {
    
            Logger.log("CheckingProcess", "init", "with url: \(url.absoluteString)")
            self.status = CheckingStatus.inactive;
            self.url = url;
            self.webView = webView
        }
    
        func startCheck(){
    
            Logger.log("CheckingProcess", "startCheck", "with url: \(self.url.absoluteString), START")
            self.status = CheckingStatus.processing
    
            let destinationUrl = Util.generateLocalFileURL(remoteURL: self.url)
    
            DownloadManager.downloadFileAndSaveIfNotHTML(url: url, to: destinationUrl, completion: {(finalLocalURL, errorMessage) in
    
                Logger.log("CheckingProcess", "startCheck callback block", "url: \(self.url.absoluteString), FinalLocalURL: \(String(describing: finalLocalURL)), errorMessage: \(String(describing: errorMessage))")
                self.filePath = finalLocalURL
                self.errorMessage = errorMessage
    
                self.status = CheckingStatus.finished
    
                self.webView?.loadRequest(NSURLRequest(url: self.url as URL) as URLRequest)
            })
        }
    
        func clearResults(){
            self.status = CheckingStatus.inactive
            self.filePath = nil
            self.errorMessage = nil
        }
    
        public var description: String {
    
            return "URL: \(self.url), Status: \(self.status), filePath: \(String(describing: self.filePath)), errorMessage: \(String(describing: self.errorMessage))"
        }
    
    }