Error form in html5 in a webview from swift

0

This is done by an app that displays a html form within a webview . In the form I have the following fields:

  • Title
  • Description
  • Image (input file)

If I do not add an image, everything is fine, but if I select an image through the input, I only reload the form, so it erases the previously entered data.

My html:

<div class="container">
    <form role="form" id="commentForm" action="/new" method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label>Titulo</label>
            <input type="text" class="form-control" name="titulo" required="">
        </div>
        <div class="form-group">
            <label>Descripcion</label>
            <textarea name="desc" style="height: 100px; width: 100%; resize: none;" required=""></textarea>
        </div>
        <div class="form-group">
            <label>Imagen</label>
            <input type="file" class="form-control" name="nombre_archivo" >
        </div>
        <button class="btn btn-success" type="submit">Crear Oferta</button>
    </form>
</div>

My swift code:

class NavegadorViewController: UIViewController, UIWebViewDelegate  {

    var url: String?

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var carga: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.webView.delegate = self
        carga.startAnimating()

    }

    func webViewDidStartLoad(webView: UIWebView) {
        carga.startAnimating()
    }

    func webViewDidFinishLoad(webView: UIWebView){
        carga.stopAnimating()
        carga.hidden = true
    }

    override func viewDidAppear(animated: Bool) {
        let url:NSURL? = NSURL(string: self.url!)
        if let actualUrl = url {
            let urlRequest:NSURLRequest = NSURLRequest(URL: actualUrl)
            self.webView.loadRequest(urlRequest)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
        if self.presentedViewController != nil {
            super.dismissViewControllerAnimated(flag, completion: completion)
        }
    }
}

What could be the problem ?? I have to change something from html or swift ??

    
asked by 29.08.2016 в 14:33
source

1 answer

0

I found the answer here .

The error was that I had to load the webview in the function viewDidAppear , then when I selected a file, when I returned to appreciate the browser again used that function, now the code I have put in the function: viewDidLoad

My code is now like this:

class NavegadorViewController: UIViewController, UIWebViewDelegate  {

    var url: String?

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var carga: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        self.webView.delegate = self
        carga.startAnimating()

        let url:NSURL? = NSURL(string: self.url!)
        if let actualUrl = url {
            let urlRequest:NSURLRequest = NSURLRequest(URL: actualUrl)
            self.webView.loadRequest(urlRequest)
        }
    }

    func webViewDidStartLoad(webView: UIWebView) {
        carga.startAnimating()
    }

    func webViewDidFinishLoad(webView: UIWebView){
        carga.stopAnimating()
        carga.hidden = true
    }

    override func viewDidAppear(animated: Bool) {
        /*let url:NSURL? = NSURL(string: self.url!)
        if let actualUrl = url {
            let urlRequest:NSURLRequest = NSURLRequest(URL: actualUrl)
            self.webView.loadRequest(urlRequest)
        }*/
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
        if self.presentedViewController != nil {
            super.dismissViewControllerAnimated(flag, completion: completion)
        }
    }
}
    
answered by 30.08.2016 / 08:41
source