4

I have an html input type="file" upload form on a page loaded through wkwebview, and I'm trying to allow the ability to upload common document files (pdf, docx, etc). Currently I have the ability to take pictures and browse for images, but documents are greyed out and unavailable. I'm a web developer, and swift is greek to me, so any help would be greatly appreciated!

Here is my view controller

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

}
Jay Patel
  • 2,346
  • 2
  • 17
  • 33
Justin M
  • 103
  • 1
  • 8

1 Answers1

2

For OSX, You can try to implement the delegate method runOpenPanelWithParameters. It is a WKUIDelegate method.

Check the accepted answer for a similar question: Input type=file not working in WebView of OS X application

  • Thanks for the reply. Unfortunately I wasn't able to get it to work. From what I've read, NSOpenPanel and WKOpenPanelParameters don't seem to be available in iOS, only OSX. I can't believe it isn't possible to upload a non-image through an iOS app? I did see some recommendations for a library like https://github.com/marmelroy/FileBrowser , but integrating it into wkwebview is beyond my abilities. Again, any help is super appreciated, and thank you again for replying. – Justin M Mar 09 '18 at 22:13
  • I'm sorry, Yes it is only available for OS X and not for iOS. Try to use real device to test this and make sure the function you want is available and working in safari. – SoftwareDeveloper Mar 21 '18 at 11:21