3

In swift 2.1, Xcode7.1.1

My code loads a local index.html into a WKwebView. How can I have a reference to the textFields so that I can set some of their properties? like myWebViewTextField.userInteractionEnabled = false or myWebViewTextField.enabled = false

The docs says:

You get a window WebScriptObject object by sending windowScriptObject to your WebView object.

I am not sure how to go from here. Thank

import UIKit
import WebKit

class ViewController: UIViewController {
@IBOutlet var containerView: UIView! = nil  //allows the class to refrence WKWebView
var webView: WKWebView?

override func loadView() {
    super.loadView()

    self.webView = WKWebView()
    self.view = self.webView!
}

override func viewDidLoad() {
    super.viewDidLoad()
    //path explained http://www.cs.tut.fi/~jkorpela/fileurl.html
    let baseUrl = NSURL(string: "file:///<path>/")
    let path = NSBundle.mainBundle().pathForResource("index", ofType: "html")
    let HTMLString: NSString?

    do {
        HTMLString = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
        webView!.loadHTMLString(HTMLString as! String, baseURL: baseUrl )

    } catch {
        HTMLString = nil
    }
}
Maciek Czarnik
  • 5,950
  • 2
  • 37
  • 50
Fred J.
  • 5,759
  • 10
  • 57
  • 106

1 Answers1

3

You're reading the wrong class' docs. WebView and WKWebView are not the same. The latter is faster and more secure but not as manipulable.

For WKWebView, you'd want to use wkwebview.evaluateJavaScript to send a whole string of code that does the manipulations to the DOM. The completionHandler can asynchronously receive simple results like strings and numbers and fire off more JS evaluations based on those, but you can't receive DOM element references and directly tweak their properties from Swift.

kfix
  • 628
  • 4
  • 12
  • Thanks for that, I need to set 'myWebViewTextField.userInteractionEnabled = false' so that the iOS keyboard gets disabled, can I do that via wkwebview.evaluateJavaScript? I will update the question. :) – Fred J. Jan 05 '16 at 04:08
  • You can't get at the underlying UIKit element that WebKit may-or-not be using - that other answer was literally about a UITextField, which your element is not. – kfix Jan 05 '16 at 07:16
  • You could use send a javascript command like `textBox.hidden = !textBox.hidden` to the webview to toggle the the text elements visibility. or use .blur() to remove focus. But you cannot bypass the iOS keyboard in a WKWebView if you use normal HTML text elements to accept input. – kfix Jan 05 '16 at 07:25
  • There is a private header for the _WKInputDelegate protocol (wkwebview._formDelegate) that has a method `func _webView(webView: WKWebView, focusShouldStartInputSession`) that can allow you to suppress the keyboard. https://github.com/WebKit/webkit/blob/833ed3dae1ec62fcb9a4c5dbb42bfba94c55f61a/Source/WebKit2/UIProcess/API/Cocoa/_WKInputDelegate.h#L45 – kfix Apr 02 '16 at 03:49
  • As 2022 the link is dead. – Pedro Paulo Amorim Aug 29 '22 at 15:01