允许在WKWebView上的输入文件中上传文档

时间:2018-01-23 22:33:30

标签: ios swift wkwebview wkwebviewconfiguration

我有一个html输入类型="文件"通过wkwebview加载的页面上传表单,我试图允许上传常见文档文件(pdf,docx等)。目前,我可以拍摄照片并浏览图像,但文档显示为灰色且不可用。我是一名网络开发人员,对我来说很快就是希望,所以任何帮助都会非常感激!

这是我的视图控制器

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)
    }
}

}

1 个答案:

答案 0 :(得分:0)

就我而言,在文件上传后,页面被重定向到基本网址。
问题是我正在调用

webview.load(request)

来自 viewDidAppear() 函数。
将调用移至 viewDidLoad() 解决了我的问题。
Click here to see more details