4

我在通过 wkwebview 加载的页面上有一个 html input type="file" 上传表单,并且我试图允许上传常见文档文件(pdf、docx 等)的能力。目前我可以拍照和浏览图片,但是文档是灰色的并且不可用。我是一名网络开发人员,对我来说 swift 是希腊语,所以任何帮助将不胜感激!

这是我的视图控制器

import UIKit
import WebKit
import IJProgressView
import FirebaseMessaging

class ViewController: UIViewController, WKNavigationDelegate {

let webView = WKWebView()
var initialstart = true



override func viewDidLoad() {

    webView.backgroundColor = UIColor(red:0.17, green:0.24, blue:0.31, alpha:1.0)
    webView.isOpaque = false
    webView.allowsBackForwardNavigationGestures = true

    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.receivedUrlFromPushNotification(notification:)), name: NSNotification.Name(rawValue: "ReceivedPushNotification"), object: nil)
    // Do any additional setup after loading the view, typically from a nib.
    guard let url = URL(string: "https://www.website.com/users/account") else { return};
    webView.frame = view.bounds;
    webView.navigationDelegate = self;
    webView.load(URLRequest(url: url));
    webView.autoresizingMask = [.flexibleWidth, .flexibleHeight];
    view.addSubview(webView)

}

func receivedUrlFromPushNotification (notification: Notification)  {
    let notification: [AnyHashable : Any] = notification.userInfo!
    let url = URL(string: notification["url"] as! String);
    webView.load(URLRequest(url: url!))

}


func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if navigationAction.navigationType == .linkActivated {
        if let url = navigationAction.request.url,
            let host = url.host, !host.hasPrefix("www.website.com"),
            UIApplication.shared.canOpenURL(url) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url)
            } else {
                // Fallback on earlier versions
                UIApplication.shared.openURL(url)
            }
            decisionHandler(.cancel)
        } else {
            decisionHandler(.allow)

        }
    } else {
        decisionHandler(.allow)
    }

}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    let url = webView.url;
    let path = url?.path;
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
    IJProgressView.shared.hideProgressView()
    view.isOpaque = true
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    if (initialstart == true) {
        initialstart = false
    } else {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        IJProgressView.shared.showProgressView(view)
    }
}

}
4

2 回答 2

2

对于 OSX,您可以尝试实现委托方法 runOpenPanelWithParameters。这是一个 WKUIDelegate 方法。

检查接受的类似问题的答案:Input type=file not working in WebView of OS X application

于 2018-03-04T09:16:57.920 回答
0

就我而言,文件上传后,页面被重定向到基本 url。
问题是我在打电话

webview.load(request)

来自viewDidAppear()函数。
将调用移至viewDidLoad()解决了我的问题。
点击这里查看更多详情

于 2021-08-04T20:22:48.210 回答